Constructors and prototypes
From Yate Documentation
(Difference between revisions)
m |
(→Accessing the prototype) |
||
Line 45: | Line 45: | ||
* [http://www.2ality.com/2010/12/javascripts-prototypal-inheritance.html An easy way to understand JavaScript’s prototypal inheritance] | * [http://www.2ality.com/2010/12/javascripts-prototypal-inheritance.html An easy way to understand JavaScript’s prototypal inheritance] | ||
* [http://joost.zeekat.nl/constructors-considered-mildly-confusing.html Constructors considered mildly confusing] | * [http://joost.zeekat.nl/constructors-considered-mildly-confusing.html Constructors considered mildly confusing] | ||
+ | |||
+ | [[Category:Javascript]] [[Category:Programmers]] |
Latest revision as of 16:32, 31 October 2013
The Javascript language does not have classes, it supports only objects. However, a common behavior of objects can be implemented using prototypes.
[edit] Adding class properties and methods
Even if objects are not class members they can share a set of common properties and methods.
Currently Yate does not automatically provide a prototype for objects. This can be added easily to a constructor:
function MyConstructor() { this.MyProperty = 0; } MyConstructor.prototype = new Object;
Now shared properties and methods can be added to the prototype:
MyConstructor.prototype.SharedProperty = 1; MyConstructor.prototype.MyMethod = function(x) { return x + this.MyProperty; };
IMPORTANT: There's a semicolon after the closing brace as this is an assignment and not a function declaration
[edit] Accessing the prototype
Javascript does not provide a standard way of accessing an object's prototype.
Yate uses a widespread extension of a __proto__ attribute to access the prototype:
Engine.print_r(this.__proto__);
See also
External links