English 中文(简体)
就此、全球目标和全球范围进行整合
原标题:Confusion about this, global object and global scope

阅读第4章关于功能的“Javascript:the goods part”时,谈到“The means insign patterns”和“The function insign patterns”(第28页)。

When a function is stored as a property of an object, we call it a method. When a method is invoked, this is bound to that object.

这一点很清楚。 随后在同一页:

When a function is not the property of an object, then it is invoked as a function:
var sum = add(3, 4); // sum is 7
When a function is invoked with this pattern, this is bound to the global object. This was a mistake in the design of the language.

我的问题:

  • 1.- With "this is bound to the global object" means that this is bound to the global scope?
  • 2.- With "This was a mistake in the design of the language" means that this should be follow the same rules as the methods follow? ie, bound this to himself?

感谢。 -

www.un.org/Depts/DGACM/index_spanish.htm 所提供的两个答复都很有启发性,而且有充分记录。 感谢所有与会者。

最佳回答

1 - Yes

2 如果你读,就在他描述一个在语言设计上错误的例子之后。

var myObject = {
  value: 1
};

myObject.double = function() {
  var that = this;

  var helper = function() {
    alert( in helper, this.value =   + this.value +   and that.value =   + that.value);
    that.value = that.value + that.value;
  }

  helper();
};

myObject.double();
alert( after doubling, myObject.value =   + myObject.value);

警告将表明,在作为物体财产的职能中使用这一规定不会奏效。 如果全球物体(如窗户)有一个<代码>,,那么这一提示就会看到这一点。

问题回答

<代码>window是 Java本的全球物体。 如果你确定这样的职能:

function myFunc() {
}

It will be bound to the global object, window and using this inside the function will refer to the window object. If you define a function as a property of an object, such as:

var obj = {
   myMethod: function() {
   }
};

并且使用<条码>,即

Here is an example: http://jsfiddle.net/williamvanr/qBXF8/





相关问题
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.

热门标签