[英]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." );
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”是对象属性的名称,其值为匿名函数(也是对象)。
The function inverts the value of isSharp
(so from true
to false
or false
to true
). 该函数反转
isSharp
的值(因此从true
到false
或false
变为true
)。
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的东西(除了undefined
, null
, false
, 0
之外几乎所有东西)。 In this case, since isSharp
is always either true
or false
, it is asserting that it is false
. 在这种情况下,由于
isSharp
始终为true
或false
,因此断言它为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.use
是Katana
一个属性,而Katana.use
的值是一个函数。 use
is a key defined on Katana
since Katana["use"]
also works. 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 === false
which it should be since it was orginally true but then set to false. 3)断言是说
katana.isSharp === false
它应该是因为它是orginally true但是然后设置为false。
use
is a property of the object katana
. use
是对象katana
的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.