Example properties versus methods in JS

I found an excellent description of the semantic difference between Properties and Methods (rephrased through http://www.webdeveloper.com/forum/showthread.php?133712-Properties-Vs.-Methods ):

Properties are similar to nouns. They matter or state.

Methods are similar to verbs. They perform actions.

A property cannot perform an action, and the only value that the method has is the one that is returned after the action completes.

eg.

Property : door; Possible values : open, closed

Method : openDoor; Action : change the value of the door property to "open"

Creating an example: I understand this theoretically, but I cannot come up with an example. Can anyone show me how the / openDoor door will look in the Javascript code itself?

+5
source share
2 answers

Indeed, you need to back up and read some of the links above. But as a quick example:

var house = {} ;

house.isDoorOpen = false ;

house.openDoor = function(){
    house.isDoorOpen = true ;
}

Here houseis the object. He has the ability to: house.isDoorOpen. Here it is more like an adjective. Either the door is open (true) or closed (false). As it sounds, he describes the property of the house.

In addition, it has a method openDoor(which is used as follows:) house.openDoor(). This is what he can do. In this case, the action openDooraffects the property isDoorOpen, making it true.

+14

, javascript ECMA-262 term

http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.26

4.3.26

,

. ( , ) .

4.3.27

. , .

Javascript

4.3.29

,


for in, , ,

http://eloquentjavascript.net/1st_edition/chapter8.html

" , , , object.method()."

, .

https://en.wikipedia.org/wiki/Property_(programming)#JavaScript

", - , , ( ) ..... - , Java, ."

javascript,

# , Java

+1

All Articles