javascript框架设计之类工厂(3)

var P = (function(prototype, ownProperty, undefined) { return function P(_superclass /* = Object */, definition) { // handle the case where no superclass is given if (definition === undefined) { definition = _superclass; _superclass = Object; } // C is the class to be returned. // // When called, creates and initializes an instance of C, unless // `this` is already an instance of C, then just initializes `this`; // either way, returns the instance of C that was initialized. // // TODO: the Chrome inspector shows all created objects as `C` // rather than `Object`. Setting the .name property seems to // have no effect. Is there a way to override this behavior? function C() { var self = this instanceof C ? this : new Bare; self.init.apply(self, arguments); return self; } // C.Bare is a class with a noop constructor. Its prototype will be // the same as C, so that instances of C.Bare are instances of C. // `new MyClass.Bare` then creates new instances of C without // calling .init(). function Bare() {} C.Bare = Bare; // Extend the prototype chain: first use Bare to create an // uninitialized instance of the superclass, then set up Bare // to create instances of this class. var _super = Bare[prototype] = _superclass[prototype]; var proto = Bare[prototype] = C[prototype] = C.p = new Bare; // pre-declaring the iteration variable for the loop below to save // a `var` keyword after minification var key; // set the constructor property on the prototype, for convenience proto.constructor = C; C.extend = function(def) { return P(C, def); } return (C.open = function(def) { if (typeof def === 'function') { // call the defining function with all the arguments you need // extensions captures the return value. def = def.call(C, proto, _super, C, _superclass); } // ...and extend it if (typeof def === 'object') { for (key in def) { if (ownProperty.call(def, key)) { proto[key] = def[key]; } } } // if no init, assume we're inheriting from a non-Pjs class, so // default to using the superclass constructor. if (!('init' in proto)) proto.init = _superclass; return C; })(definition); } // as a minifier optimization, we've closured in a few helper functions // and the string 'prototype' (C[p] is much shorter than C.prototype) })('prototype', ({}).hasOwnProperty);

我们尝试创建一个类:

var Dog = P (function(proto, superProto){ proto.init = function(name) { //构造函数 this.name = name; } proto.move = function(meters){ //原型方法 console.log(this.name + " moved " + meters + " m.") } }); var a = new Dog("aaa") var b = new Dog("bbb"); //无实例变化 a.move(1); b.move(2);

我们在现在的情况下,可以尝试创建更简洁的定义方式

var Animal = P (function(proto, superProto){ proto.init = function(name) { //构造函数 this.name = name; } proto.move = function(meters){ //原型方法 console.log(this.name + " moved " + meters + " m.") } }); var a = new Animal("aaa") var b = new Animal("bbb"); //无实例变化 a.move(1); b.move(2); //............... var Snake = P (Animal, function(snake, animal){ snake.init = function(name, eyes){ animal.init.call(this, arguments); //调运父类构造器 this.eyes = 2; } snake.move = function() { console.log('slithering...'); animal.move.call(this, 5); //调运父类同名方法 } }); var s = new Snake("snake", 1); s.move(); console.log(s.name); console.log(s.eyes);

私有属性演示,由于放在函数体内集中定义,因此安全可靠!

var Cobra = P (Snake, function(cobra){ var age = 1;//私有属性 //这里还可以编写私有方法 cobra.glow = function(){ //长大 return age++; } }); var c = new Cobra("cobra"); console.log(c.glow()); //1 console.log(c.glow()); //2 console.log(c.glow()); //3

以上所述就是本文的全部内容了,希望大家能够喜欢。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgdffy.html