javascript JS 中的属性与方法示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14953047/
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
Example of Properties vs. Methods in JS
提问by jon
I found a great description of the semantic difference between Properties and Methods (paraphrased, via http://www.webdeveloper.com/forum/showthread.php?133712-Properties-Vs.-Methods):
我发现了对属性和方法之间语义差异的很好的描述(转述,通过http://www.webdeveloper.com/forum/showthread.php?133712-Properties-Vs.-Methods):
Properties are like nouns. They have a value or state.
Methods are like verbs. They perform actions.
A property can't perform an action and the only value that a method has is the one that is returned after it finishes performing the action.
e.g.
Property: door; Possible Values: open, closed
Method: openDoor; Action: to change the value of the door property to "open"
属性就像名词。它们有一个值或状态。
方法就像动词。他们执行操作。
属性不能执行操作,并且方法具有的唯一值是它完成执行操作后返回的值。
例如
属性:门;可能的值:打开、关闭
方法:打开门;操作:将门属性的值更改为“打开”
Creating an example:I understand this in theory but I can't come up with an example. Would it be possible to show me how the door/openDoor would look in actual Javascript code?
创建一个例子:我在理论上理解这一点,但我无法举出一个例子。是否可以向我展示在实际 Javascript 代码中门/openDoor 的外观?
回答by ColBeseder
Really, you need to back up and read some of the links posted above. But as a quick example:
真的,您需要备份并阅读上面发布的一些链接。但作为一个简单的例子:
var house = {} ;
house.isDoorOpen = false ;
house.openDoor = function(){
house.isDoorOpen = true ;
}
Here house
is the object. It has a property: house.isDoorOpen
. Here, it is more like an adjective. Either the door is open (true) or closed (false). As it sounds, it describes a property of the house.
这里house
是对象。它有一个属性:house.isDoorOpen
。在这里,它更像是一个形容词。门是打开的(真)或关闭的(假)。听起来,它描述了房子的财产。
Also, it has a method openDoor
(which is used like this: house.openDoor()
). That's something that it can do. In this case, the action openDoor
affects the isDoorOpen
property, making it true.
此外,它有一个方法openDoor
(使用方式如下:)house.openDoor()
。这是它可以做的事情。在这种情况下,操作openDoor
会影响isDoorOpen
属性,使其为真。
回答by barlop
Let's look at how the javascript spec ECMA-262 describes the term property
我们来看看 javascript 规范 ECMA-262 如何描述术语属性
http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.26
http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.26
4.3.26 property
association between a name and a value that is a part of an object
NOTE Depending upon the form of the property the value may be represented either directly as a data value (a primitive value, an object, or a function object) or indirectly by a pair of accessor functions.
4.3.27 method
function that is the value of a property
NOTE When a function is called as a method of an object, the object is passed to the function as its this value.
4.3.26 财产
名称和作为对象一部分的值之间的关联
注根据属性的形式,值可以直接表示为数据值(原始值、对象或函数对象),也可以由一对访问器函数间接表示。
4.3.27 方法
作为属性值的函数
注意当一个函数作为一个对象的方法被调用时,该对象作为它的 this 值传递给函数。
Also
还
Javascript's definition of attribute is different from Java's
Javascript 对属性的定义与 Java 不同
4.3.29 attribute
internal value that defines some characteristic of a property
4.3.29 属性
定义属性的某些特征的内部值
for in
, loops through an object's enumerable properties, and that includes its functions
for in
, 循环遍历对象的可枚举属性,包括其函数
http://eloquentjavascript.net/1st_edition/chapter8.html
http://eloquentjavascript.net/1st_edition/chapter8.html
"A function is called as a method when it is looked up as a property, and immediately called, as in object.method()."
“当一个函数作为一个属性被查找时,它被作为一个方法调用,并立即被调用,就像在 object.method() 中一样。”
There does seem to be a more standard definition of property..
似乎确实有一个更标准的财产定义。
https://en.wikipedia.org/wiki/Property_(programming)#JavaScript
https://en.wikipedia.org/wiki/Property_(编程)#JavaScript
"A property, in some object-oriented programming languages, is a special sort of class member, intermediate between a field (or data member) and a method. .... Some object-oriented languages, such as Java, don't support properties, and require the programmer to define a pair of accessor and mutator methods instead."
“在某些面向对象的编程语言中,属性是一种特殊的类成员,介于字段(或数据成员)和方法之间......一些面向对象的语言,例如 Java,不支持属性,并要求程序员定义一对访问器和修改器方法。”
In that more standard, non-javascript definition of property
在更标准的非 JavaScript 属性定义中
C# has properties, and Java doesn't have properties
C#有属性,而Java没有属性