我有类似的东西
var foo = function(arg){
var something = {
myPropVal: "the code",
myMethodProp: function(bla) {
// do stuff with mypropval here
alert(this) // => DOMWindow
}
}
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?我可以从 myMethodProp 中访问 myPropVal 的内容吗?
是的,可以,下面是一个例子。
obj = {
offset: 0,
IncreaseOffset: function (num) {
this.offset += num
},
/* Do not use the arrow function. Not working!
IncreaseOffset2: (num) => {
this.offset += num
}
*/
}
obj.IncreaseOffset(3)
console.log(obj.offset) // 3Run Code Online (Sandbox Code Playgroud)
你当然可以
var foo = function(arg){
var something = {
myPropVal: "the code",
myMethodProp: function(bla) {
// do stuff with mypropval here
alert(this) // => DOMWindow
alert(this.myPropVal);
}
}
alert(something.myMethodProp());
}
foo();
Run Code Online (Sandbox Code Playgroud)