Created
August 8, 2011 04:56
-
-
Save ambar/1131230 to your computer and use it in GitHub Desktop.
whatsthis
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
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<script> | |
/* | |
jQuery.proxy和Function.prototype.bind作用一模一样。 | |
:: this.$submit.bind('click',$.proxy(this.save,this)); | |
:: callback.bind(xhr); << https://gist.github.com/1088730 | |
而Function.prototype.bind可以由Function.prototype.call或Function.prototype.apply合成而来。 | |
根据crockford的good parts: | |
函数有4种调用方式: | |
方法、函数、构造器、apply | |
this指向的区别就在调用方式的区别上面。 | |
*/ | |
var App = { | |
xmpp : { | |
connection : 'fine', | |
state : function() { | |
console.log(this.connection,this.state); | |
var test = function() { | |
console.log('inner this >>> ',this); | |
} | |
test(); | |
} | |
} | |
}; | |
// I 函数调用模式 | |
var state = App.xmpp.state; | |
state(); | |
// II 方法调用模式A | |
var xmpp = App.xmpp; | |
xmpp.state(); | |
/// III 方法调用模式B | |
App.xmpp.state(); | |
// IV | |
var logger = { | |
log : function(fn) { | |
fn(); | |
} | |
} | |
// 仍然是函数调用模式 | |
logger.log(App.xmpp.state); | |
// bind代理 apply模式 | |
logger.log( App.xmpp.state.bind(App.xmpp) ); | |
// 即 logger.log( state.bind(xmpp) ); | |
// 原始apply模式 | |
logger.log(function() { | |
state.apply(xmpp); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment