JavaScript - 作为对象属性的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5334383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:43:32  来源:igfitidea点击:

JavaScript - function as an object property

javascript

提问by mjmitche

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

大家好,这是 John Resig Advanced JavaScript http://ejohn.org/apps/learn/#23 中的#23 ,称为

What happens if a function is an object property.

What happens if a function is an object property.

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?

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

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

2)。函数的目的是将 isSharp: true 改为 isSharp: false 吗?!this.isSharp 到底是做什么的?

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

3) 当它断言 !katana.isSharp 时,它实际上断言了什么?那 isSharp 现在已被设置为“false”?

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

回答by Cameron

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

  2. The function inverts the value of isSharp(so from trueto falseor falseto true).

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

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

  2. 该函数反转isSharp(so from trueto falseor falseto true) 的值。

  3. 它声称isSharp是一些东西,不计算为真(这几乎是除了一切undefinednullfalse0,等)。在这种情况下,由于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 katanaobject (that's the katana.usepart). 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 katanaobject-- that means thisin the anonymous function is a reference to the katanaobject when it's called that way.

这首先从katana对象(即katana.use部件)中获取“use”属性的值。该值是之前的匿名函数。然后,执行该函数(这就是()部分)。真正酷的部分是它代表katana对象执行——这意味着this匿名函数在以这种方式调用时是对katana对象的引用。

回答by Raynos

1) Katanais an object. Katana.useis a function. Its a property that contains a function as value. The value it contains happens to be an anonymous function.

1)Katana是一个对象。Katana.use是一个函数。它是一个包含函数作为值的属性。它包含的值恰好是一个匿名函数。

The distinction is that Katana.useis a property of Katanaand that the value of Katana.useis a function. useis a key defined on Katanasince Katana["use"]also works.

区别在于它Katana.use是一个属性,Katana而它的值Katana.use是一个函数。use是一个定义的键,Katana因为它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 === falsewhich it should be since it was orginally true but then set to false.

3)断言katana.isSharp === false说它应该是哪个,因为它最初是真的,但后来被设置为假。

回答by Arun P Johny

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