Skip to content

Instantly share code, notes, and snippets.

@ambar
Created August 8, 2011 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambar/1131230 to your computer and use it in GitHub Desktop.
Save ambar/1131230 to your computer and use it in GitHub Desktop.
whatsthis
<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