Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created November 30, 2011 16:44
Show Gist options
  • Save rummelonp/1409749 to your computer and use it in GitHub Desktop.
Save rummelonp/1409749 to your computer and use it in GitHub Desktop.
JavaScript の this による破滅
(function() {
var Hametsu1;
Hametsu1 = (function() {
function Hametsu1() {
$(function() {
return $(document).delegate('a', 'click', this.say); // this が document を指すのであぼーん
});
}
Hametsu1.prototype.say = function() {
return window.alert(this.getName());
};
Hametsu1.prototype.getName = function() {
return 'Hametsu1';
};
return Hametsu1;
})();
}).call(this);
(function() {
var Hametsu2;
Hametsu2 = (function() {
function Hametsu2() {
var self;
self = this; // 別の変数に入れる
$(function() {
return $(document).delegate('a', 'click', self.say); // あぼーんしない
});
}
Hametsu2.prototype.say = function(e) {
return window.alert(this.getName()); // this が a を指すのであぼーん
};
Hametsu2.prototype.getName = function() {
return 'Hametsu2';
};
return Hametsu2;
})();
new Hametsu2;
}).call(this);
(function() {
var Hametsu3;
Hametsu3 = (function() {
var self;
function Hametsu3() {}
self = Hametsu3; // 別の変数に入れる
Hametsu3.say = function(e) {
return window.alert(self.getName()); // あぼーんしない
};
Hametsu3.getName = function() {
return 'Hametsu3';
};
$(function() {
return $(document).delegate('a', 'click', self.say); // あぼーんしない
});
return Hametsu3;
})();
new Hametsu3;
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment