JavaScript: fields or properties
In every JavaScript tutorial that I have looked there is something mentioned about a property of an object. But why do they cal开发者_运维技巧l it a property? e.g. constructor property, var a = function{this.b}
where b
is again called property. As far as I know properties have getter and/or setter, so those one should be called field, shouldn't they?
Some browser vendor have implemented getters/setters for JavaScript properties.
FF and Webkit has __defineGetter__
and __defineSetter__
implemented for DOM objects and get and set for Object's which is outside of the ECMA specification. However both and also IE 8+ has the Object.defineProperty
(from the ECMA specification). Read about it here
As for you original question, I would say that the reason it's called property in JavaScript is that it's a dynamic language and the basic markup uses only properties and local var's. Since everything is bound to a specific scope-hierarchy all you have is different tree-branches on each level. And Douglas Croockford named them properties :)
It's like with hash
, map
, dictionary
and associative array
; different languages like to call them different things. Terminology (unfortunately) isn't 100% transferable between programming languages.
精彩评论