![](/img/trans.png)
[英]What is the best way keep track of your identifiers for your Jquery and CSS?
[英]What the best way to keep track of a javascript object reference when prototyping
關於跟蹤javascript中的對象引用的最佳實踐是什么:
function class1(param) {
this.param = param;
self = this;
}
class1.prototype.method1 = function () {
console.log(self.param);
}
$('document').ready(function () {
object1 = new class1('foo');
object2 = new class1('bar');
object1.method1(); // expected: foo , actual: bar
})
當前self似乎是全局的,並在object2的構造函數中被覆蓋。 如果有人可以提供一些建議,我將不勝感激。
你並不需要跟蹤,只是用this
像這樣:
function class1(param) {
this.param = param;
}
class1.prototype.method1 = function () {
console.log(this.param);
}
function class1(param) {
this.param = param;
}
class1.prototype.method1 = function () {
console.log(this.param);
}
$('document').ready(function () {
var object1 = new class1('foo');
var object2 = new class1('bar');
object1.method1(); // expected: foo , actual: foo
});
為什么要添加此self
事物?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.