简体   繁体   English

JavaScript - 作为对象属性

[英]JavaScript - function as an object property

Hey everyone, this is #23 from John Resig Advanced JavaScript http://ejohn.org/apps/learn/#23 , called 嘿大家,这是来自John Resig Advanced JavaScript的#23,来自http://ejohn.org/apps/learn/#23 ,名为

What happens if a function is an object property.

1) regarding vocabulary, the variable katana is the object, right? 1)关于词汇,变量武士刀是对象,对吧? 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). 2)。 Is the purpose of the function to change isSharp: true to isSharp: false? 该函数的目的是更改为:Sharp:isSharp:false? What does !this.isSharp exactly do? 什么!this.isSharp究竟做什么?

3) When it asserts !katana.isSharp, what is it actually asserting? 3)当它断言!katana.isSharp时,它实际上断言了什么? 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. Yes, katana is an object (created using the { ... } notation). 是的, katana是一个对象(使用{ ... }符号创建)。 "use" is the name of the property of the object whose value will be the anonymous function (which is also an object). “use”是对象属性的名称,其值为匿名函数(也是对象)。

  2. The function inverts the value of isSharp (so from true to false or false to true ). 该函数反转isSharp的值(因此从truefalsefalse变为true )。

  3. It is asserting that isSharp is something which does not evaluate to true (this is nearly everything except undefined , null , false , 0 , etc). 它声称isSharp是不能评估为true的东西(除了undefinednullfalse0之外几乎所有东西)。 In this case, since isSharp is always either true or false , it is asserting that it is false . 在这种情况下,由于isSharp始终为truefalse ,因此断言它为false

The main point (and cool part) of the sample is this line: 样本的主要观点(和冷却部分)是这一行:

katana.use();

This first fetches the value of the "use" property from the katana object (that's the katana.use part). 这首先从katana对象(即katana.use部分)获取“use”属性的值。 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. 真正酷的是,它是代表执行katana的对象 -这意味着this在匿名功能是将一个参考katana对象时,它被称为这种方式。

1) Katana is an object. 1) Katana是一个对象。 Katana.use is a function. Katana.use是一个功能。 Its a property that contains a function as value. 它是一个包含函数值的属性。 The value it contains happens to be an anonymous function. 它包含的值恰好是一个匿名函数。

The distinction is that Katana.use is a property of Katana and that the value of Katana.use is a function. 区别在于Katana.useKatana一个属性,而Katana.use的值是一个函数。 use is a key defined on Katana since Katana["use"] also works. useKatana定义的一个关键,因为Katana["use"]也有效。

2) It's setting isSharp to NOT isSharp so either true -> false or false -> true 2)设置isSharp为NOT isSharp所以要么为true - > false或false - > true

3) the assert is saying katana.isSharp === false which it should be since it was orginally true but then set to false. 3)断言是说katana.isSharp === false它应该是因为它是orginally true但是然后设置为false。

  1. use is a property of the object katana . use是对象katana的属性。
  2. !this.isSharp will negate the value of this.isSharp. !this.isSharp将否定this.isSharp的价值。 Ex if isSharp is true it will return false else it return false. 如果isSharp为true,则返回false,否则返回false。
  3. The assert checks whether the result of the boolean result is true. 断言检查布尔结果的结果是否为真。 If the result is false then the assertion fails. 如果结果为false,则断言失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM