javascript - YUI: would it make sense for YAHOO.lang.extend to carry "static" methods to subclasses? -
Consider the JavaScript code below, inspired by the YUI documentation.
In this code, the class 1 named "static method" named factory ()
(say this is a type of object factory). Class 2 is obtained from Class 1 using YAHOO.lang.extend
. Why not factory ()
will automatically be defined in class 2? Is this a fair expectation or is there a better way to do this?
Yahoo Namespace ("test"); YAHOO.test.Class1 = function () {}; YAHOO.test.Class1.factory = function () {console.log ("Class 1 static"); } YAHOO.test.Class1.prototype.testMethod = function (info) {console.log ("Class 1:" + info); }; YAHOO.test.Class2 = Function (info) {YAHOO.test.Class2.superclass.constructor.call (this, info); }; YAHOO.lang.extend (YAHOO.test.Class2, YAHOO.test.Class1); YAHOO.test.Class2.prototype.testMethod = Function (info) {console.log ("Class 2:" + info); }; YAHOO.test.Class1.factory (); // Works OK YAHOO.test.Class2.factory (); // "Static Method" is not insighted
Dosstring from the method:
Utility to install prototype, constructor and superclass property to support a heritage strategy which can provide console constructor and methods. Stable members will not inherit
Emphasis mine
When you use the extension ()
method, according to your example, the legacy series will look like this:
Superclass constructor + --- ---------------------- ------ + ---------------- & gt; Class 2 () | | / | + --------- + | / + --------- + V prototype | | | / Proto | | {} & Lt; ------- | Class 1 | {} & Lt; ------- | Class 2 | | | | | | | | + --------- + | + --------- + V | V testMethod () | Test-method () v factory ()
If you want to be accessible from factory ()
class 2, then you should stick it on class1 Prototype
Comments
Post a Comment