xxxxxxxxxx
<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>