Created
October 22, 2017 13:13
-
-
Save rijine/1974824c379ad017801fce460ea2e69b to your computer and use it in GitHub Desktop.
JS Bin bind call apply // source https://jsbin.com/bawawij
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="bind call apply"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var obj = { | |
first: 3, | |
second: 5 | |
}; | |
var sum = function(a, b) { | |
return a + b; | |
}; | |
var fn = function() { | |
return sum(this.first, this.second); | |
}; | |
function speak2(fn, obj) { | |
var d = fn.bind(obj); | |
console.log('bind ', d() ); | |
console.log('call ', fn.call(obj) ); | |
console.log('apply ', fn.apply(obj) ); | |
} | |
speak2(fn, obj); | |
function speak(fn, obj) { | |
//Function.prototype | |
//console.log(fn); | |
obj.fn = fn; | |
//window.first = 3; | |
//window.second = 3; | |
return obj.fn(); | |
//var func = new fn(); | |
//console.log(func) | |
//func.prototype.first = obj.first; | |
//func.prototype.second = obj.second; // | |
//var func = new fn(); | |
//fn.first = 100; | |
//fn.second = 100; | |
//func.prototype.first = 1; | |
//func.prototype.second = 2; | |
//fn.apply(this, obj.first, obj.second); | |
//eturn fn(obj.first, obj.second); | |
//return fn(); | |
} | |
var result = speak(fn, obj); | |
console.log('simple ', result); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> var obj = { | |
first: 3, | |
second: 5 | |
}; | |
var sum = function(a, b) { | |
return a + b; | |
}; | |
var fn = function() { | |
return sum(this.first, this.second); | |
}; | |
function speak2(fn, obj) { | |
var d = fn.bind(obj); | |
console.log('bind ', d() ); | |
console.log('call ', fn.call(obj) ); | |
console.log('apply ', fn.apply(obj) ); | |
} | |
speak2(fn, obj); | |
function speak(fn, obj) { | |
//Function.prototype | |
//console.log(fn); | |
obj.fn = fn; | |
//window.first = 3; | |
//window.second = 3; | |
return obj.fn(); | |
//var func = new fn(); | |
//console.log(func) | |
//func.prototype.first = obj.first; | |
//func.prototype.second = obj.second; // | |
//var func = new fn(); | |
//fn.first = 100; | |
//fn.second = 100; | |
//func.prototype.first = 1; | |
//func.prototype.second = 2; | |
//fn.apply(this, obj.first, obj.second); | |
//eturn fn(obj.first, obj.second); | |
//return fn(); | |
} | |
var result = speak(fn, obj); | |
console.log('simple ', result);</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var obj = { | |
first: 3, | |
second: 5 | |
}; | |
var sum = function(a, b) { | |
return a + b; | |
}; | |
var fn = function() { | |
return sum(this.first, this.second); | |
}; | |
function speak2(fn, obj) { | |
var d = fn.bind(obj); | |
console.log('bind ', d() ); | |
console.log('call ', fn.call(obj) ); | |
console.log('apply ', fn.apply(obj) ); | |
} | |
speak2(fn, obj); | |
function speak(fn, obj) { | |
//Function.prototype | |
//console.log(fn); | |
obj.fn = fn; | |
//window.first = 3; | |
//window.second = 3; | |
return obj.fn(); | |
//var func = new fn(); | |
//console.log(func) | |
//func.prototype.first = obj.first; | |
//func.prototype.second = obj.second; // | |
//var func = new fn(); | |
//fn.first = 100; | |
//fn.second = 100; | |
//func.prototype.first = 1; | |
//func.prototype.second = 2; | |
//fn.apply(this, obj.first, obj.second); | |
//eturn fn(obj.first, obj.second); | |
//return fn(); | |
} | |
var result = speak(fn, obj); | |
console.log('simple ', result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment