本文介绍了JavaScript - 作为对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这是来自John Resig高级JavaScript的#23 ,名为

Hey everyone, this is #23 from John Resig Advanced JavaScript http://ejohn.org/apps/learn/#23, called

1)关于词汇,变量katana是对象,对吗?如果匿名函数是它的属性,那么所谓的使用是什么?我认为使用也会被称为财产?或者使用也是一个对象,因为它包含一个值,即一个函数?

1) regarding vocabulary, the variable katana is the object, right? If the anonymous function is its property, then what is "use" called? I thought "use" would have also been called a property? or is "use" also an object because it contains a value, namely a function?

2)。该函数的目的是更改为:Sharp:isSharp:false?什么!this.isSharp究竟做什么?

2). Is the purpose of the function to change isSharp: true to isSharp: false? What does !this.isSharp exactly do?

3)当它断言!katana.isSharp时,它实际断言了什么?那个夏普现在被设定为假?

3) When it asserts !katana.isSharp, what is it actually asserting? that isSharp has now been set to "false"?

var katana = {
  isSharp: true,
  use: function(){
    this.isSharp = !this.isSharp;
  }
};
katana.use();
assert( !katana.isSharp, "Verify the value of isSharp has been changed." );


推荐答案


  1. 是, katana 是一个对象(使用 {...} 表示法创建)。 use是对象属性的名称,其将是匿名函数(也是对象)。

  1. Yes, katana is an object (created using the { ... } notation). "use" is the name of the property of the object whose value will be the anonymous function (which is also an object).

该函数反转 isSharp 的值(所以从 true false false true )。

The function inverts the value of isSharp (so from true to false or false to true).

断言 isSharp 是不能评估为真的东西(这几乎是除了 undefined 之外的所有内容, null false 0 等)。在这种情况下,因为 isSharp 总是 true false ,它声称它是 false

It is asserting that isSharp is something which does not evaluate to true (this is nearly everything except undefined, null, false, 0, etc). In this case, since isSharp is always either true or false, it is asserting that it is false.

样本的主要观点(和酷部分)是这一行:

The main point (and cool part) of the sample is this line:

katana.use();

首先从 katana object(即 katana.use 部分)。该值是之前的匿名函数。然后,执行该函数(即()部分)。非常酷的部分是它代表 katana 对象执行 - 这意味着这个在匿名函数中是对 katana 对象的引用,当它被调用时。

This first fetches the value of the "use" property from the katana object (that's the katana.use part). The value is the anonymous function from before. Then, that function is executed (that's the () part). The really cool part is that it is executed on behalf of the katana object -- that means this in the anonymous function is a reference to the katana object when it's called that way.

这篇关于JavaScript - 作为对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:30