English 中文(简体)
Javascript Variable scope. 我如何从内部目的中获取反对?
原标题:Javascript Variable Scope. How Do I Access an Object Var From an Internal Object Function?
  • 时间:2012-04-15 00:57:13
  •  标签:
  • javascript

我不禁要问,如何获得“名称”变量,其功能是 j式物体:

function myObj() {
    this.vars = {
        name:  bob ,
        age:  42 
    }
    this.functions = {
        sayName: function() {
            alert(this.vars.name);
        }
    }
}

var obj = new myObj();
obj.functions.sayName();

我在这里失踪了什么?

如果我提及自己的话,我就能够找到名字:

function myObj() {
    this.vars = {
        name:  bob ,
        age:  42 
    }
    this.functions = {
        sayName: function(name) {
            alert(name);
        }
    }
}

var obj = new myObj();
obj.functions.sayName(obj.vars.name);

但这似乎只是多余的。

最佳回答

问题是,在您的职衔上,this.Functions.sayName,this, 关键词指this.Functions, 而不是现有标的1

下面是这一问题的定点:

function myObj() {
    var vars = this.vars = {
        name:  bob ,
        age:  42 
    }
    this.functions = {
        sayName: function() {
            alert(vars.name);
        }
    }
}

var obj = new myObj();
obj.functions.sayName();

这在<代码>myObj内形成一个当地变量,使您能够从任何内部范围进入。 这还暴露出,外部世界的变数是任何瞬时的<编码>myObj类别的财产,这意味着你仍然可以做一些事情。

obj.vars.name =  test ;

它将继续改变物体的内部状况。 这或许是你目前工作的最佳解决办法。

<>1> The conduct of the this keyword in Javagust is interest. 我建议在航天员网络中提供优良的write

问题回答

你可以这样做:

var thisObj = this;
this.functions = {
    sayName: function() {
        alert(thisObj.vars.name);
    }
}

不能确定这一解决办法是否可行,而是行之有效。 Java稿(this)如果在职能范围内重新运作,总是指内部范围。

仅删除<条码>。

function myObj() {
    var vars = {
        name:  bob ,
        age:  42 
    };
    this.functions = {
        sayName: function() {
            alert(vars.name);
        }
    };
};

var obj = new myObj();
obj.functions.sayName();​

LIVE DEMO<





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签