开发者

Javascript Properties Overloading ala PHP?

I'm trying to find a way to to property overloading like it's done in PHP: http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

i.e.

var myobj = function () {}
myobj.prototype.getProperty = function (propertyName) { console.log('Property Requested: ', propertyName); }

开发者_如何学Pythonvar otherObj = function () {};

myobj.hello; // Property Request: hello
otherObj.hello; // undefined

Is this possible?


This sort of thing can only be done in ECMAscript 5 which is not supported in all browsers (e.g. IE). Using Object.defineProperty you can create properties with accessor functions - so you could implement a property like length in arrays that varies, depending on the object state.

There's a good presentation from Doug Crockford about these features and with links to more detailed descriptions here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜