Example of Properties vs. Methods in JS

前端 未结 2 1529
醉话见心
醉话见心 2020-12-08 05:44

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

2条回答
  •  醉梦人生
    2020-12-08 06:17

    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.

    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.

提交回复
热议问题