首页 > 代码库 > (1) basic javascript mvc pattern

(1) basic javascript mvc pattern

var Class = function(){        var klass = function(){            this.init.apply(this,arguments);        };        //init        klass.prototype.init = function(){};        return klass;    };    var Person = new Class();    Person.prototype.init = function(){        this.name = "Jackey";    };    Person.prototype.find = function(){        console.log(this.name);    };        var person = new Person();    person.find();


(1)

var Person = new Class();


为了制造出这样的效果,我们要先写一个Class的function,实例化后返回一个function:

var Class = function(){        var klass = function(){};        return klass;    };

(2) 给返回function 添加一个程序的入口

var Class = function(){        var klass = function(){            this.init.apply(this,arguments);        };        //init        klass.prototype.init = function(){};        return klass;    };

这就意味着,返回的function原型的init的this指针变量都会被引用到klass里面

(3)编写类,通过原型去扩展自己的function

 var Person = new Class();    Person.prototype.init = function(){        this.name = "Jackey";    };    Person.prototype.find = function(){        console.log(this.name);    };    var person = new Person();    person.find();

 

(1) basic javascript mvc pattern