javascript: 'this' 对象不可访问?
javascript: 'this' object not accessible?
我正在使用 here 中的示例。
function Restaurant() {
this.mongoose = 'beans';
this.freedom = {bear: 'love', a: 12};
var myPrivateVar;
// Only visible inside Restaurant()
var private_stuff = function() {
myPrivateVar = "I can set this here!";
this.mongoose = 12;
this.freedom.a = 14; // <= 'a' undefined 2nd time
}
// use_restroom is visible to all
this.use_restroom = function() {
private_stuff();
}
// buy_food is visible to all
this.buy_food = function() {
private_stuff();
}
private_stuff.call(this);
}
var bobbys = new Restaurant();
bobbys.buy_food() // <= fails
我在 private_stuff()
中将 this.freedom.a
设置为 14,然后调用 bobbys.buy_food()
。
当调用该函数时,this.freedom
显然是未定义的,所以我无法设置 a
.
有人知道为什么它在 freedom 上失败而不在 mongoose 上失败吗?
感谢评论,但如果有人发帖 'Answer',那么我可以将此作为已解决关闭,并感谢您。
function Restaurant() {
var ths = this;
this.mongoose = 'beans';
this.freedom = {bear: 'love', a: 12};
var myPrivateVar;
// Only visible inside Restaurant()
var private_stuff = function() {
myPrivateVar = "I can set this here!";
this.mongoose = 12;
ths.freedom.a = 14; // <= 'a' undefined 2nd time
}
// use_restroom is visible to all
this.use_restroom = function() {
private_stuff();
}
// buy_food is visible to all
this.buy_food = function() {
private_stuff();
}
private_stuff.call(this);
}
var bobbys = new Restaurant();
bobbys.buy_food()
试试这个
现在应该可以了:)
只需在您的购买食物方法中使用调用即可。
function Restaurant() {
this.mongoose = 'beans';
this.freedom = {bear: 'love', a: 12};
var myPrivateVar;
// Only visible inside Restaurant()
var private_stuff = function() {
myPrivateVar = "I can set this here!";
this.mongoose = 12;
this.freedom.bear = 14; // <= 'a' undefined 2nd time
}
// use_restroom is visible to all
this.use_restroom = function() {
private_stuff();
}
// buy_food is visible to all
this.buy_food = function(...args) {
private_stuff.call(this, ...args);
}
private_stuff.call(this);
}
var bobbys = new Restaurant();
bobbys.buy_food()
如果您有兴趣正确使用此模式,其重点是完全避免 this
(更不用说 .call(this)
)。您的“私有”方法和属性只是闭包中的变量,并且 public methods/props 附加到本地对象,该对象可以是封闭的 this
也可以只是文字:
function Restaurant() {
var self = this; // or {}
self.publicProp = '';
var privateProp = '';
var privateFunc = function() {
privateProp = 'hello';
self.publicProp = 'world';
}
self.publicFunc = function() {
privateFunc();
return privateProp;
}
// return self; // if used a literal above
}
var bobbys = new Restaurant();
console.log(bobbys.publicFunc())
console.log(bobbys.publicProp)
我正在使用 here 中的示例。
function Restaurant() {
this.mongoose = 'beans';
this.freedom = {bear: 'love', a: 12};
var myPrivateVar;
// Only visible inside Restaurant()
var private_stuff = function() {
myPrivateVar = "I can set this here!";
this.mongoose = 12;
this.freedom.a = 14; // <= 'a' undefined 2nd time
}
// use_restroom is visible to all
this.use_restroom = function() {
private_stuff();
}
// buy_food is visible to all
this.buy_food = function() {
private_stuff();
}
private_stuff.call(this);
}
var bobbys = new Restaurant();
bobbys.buy_food() // <= fails
我在 private_stuff()
中将 this.freedom.a
设置为 14,然后调用 bobbys.buy_food()
。
当调用该函数时,this.freedom
显然是未定义的,所以我无法设置 a
.
有人知道为什么它在 freedom 上失败而不在 mongoose 上失败吗?
感谢评论,但如果有人发帖 'Answer',那么我可以将此作为已解决关闭,并感谢您。
function Restaurant() {
var ths = this;
this.mongoose = 'beans';
this.freedom = {bear: 'love', a: 12};
var myPrivateVar;
// Only visible inside Restaurant()
var private_stuff = function() {
myPrivateVar = "I can set this here!";
this.mongoose = 12;
ths.freedom.a = 14; // <= 'a' undefined 2nd time
}
// use_restroom is visible to all
this.use_restroom = function() {
private_stuff();
}
// buy_food is visible to all
this.buy_food = function() {
private_stuff();
}
private_stuff.call(this);
}
var bobbys = new Restaurant();
bobbys.buy_food()
试试这个
现在应该可以了:)
只需在您的购买食物方法中使用调用即可。
function Restaurant() {
this.mongoose = 'beans';
this.freedom = {bear: 'love', a: 12};
var myPrivateVar;
// Only visible inside Restaurant()
var private_stuff = function() {
myPrivateVar = "I can set this here!";
this.mongoose = 12;
this.freedom.bear = 14; // <= 'a' undefined 2nd time
}
// use_restroom is visible to all
this.use_restroom = function() {
private_stuff();
}
// buy_food is visible to all
this.buy_food = function(...args) {
private_stuff.call(this, ...args);
}
private_stuff.call(this);
}
var bobbys = new Restaurant();
bobbys.buy_food()
如果您有兴趣正确使用此模式,其重点是完全避免 this
(更不用说 .call(this)
)。您的“私有”方法和属性只是闭包中的变量,并且 public methods/props 附加到本地对象,该对象可以是封闭的 this
也可以只是文字:
function Restaurant() {
var self = this; // or {}
self.publicProp = '';
var privateProp = '';
var privateFunc = function() {
privateProp = 'hello';
self.publicProp = 'world';
}
self.publicFunc = function() {
privateFunc();
return privateProp;
}
// return self; // if used a literal above
}
var bobbys = new Restaurant();
console.log(bobbys.publicFunc())
console.log(bobbys.publicProp)