从javascript中的对象方法访问对象属性
我有点像从javascript中的对象方法访问对象属性,javascript,Javascript,我有点像 var foo = function(arg){ var something = { myPropVal: "the code", myMethodProp: function(bla) { // do stuff with mypropval here alert(this) // => DOMWindow } } } 这可能吗?我可以从myMethodProp中访问myPropVal的内容吗?如果给定您可能必须将其引用
var foo = function(arg){
var something = {
myPropVal: "the code",
myMethodProp: function(bla) {
// do stuff with mypropval here
alert(this) // => DOMWindow
}
}
}
这可能吗?我可以从myMethodProp中访问myPropVal的内容吗?如果给定您可能必须将其引用为
某物。myPropVal
您可能必须将其引用为某物。myPropVal
当然可以
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();
当然可以
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();
因此实际上,
这个
不是DOMWindow。这个是什么取决于函数调用的上下文。因此,显式使用某物
可能更安全。因此,实际上此
不是DOMWindow。此是什么取决于调用函数的上下文。因此,明确使用某些东西可能会更安全。