码迷,mamicode.com
首页 > 编程语言 > 详细

(1) basic javascript mvc pattern

时间:2014-09-10 21:00:41      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   java   ar   div   cti   sp   

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

标签:style   blog   color   io   java   ar   div   cti   sp   

原文地址:http://www.cnblogs.com/lihaozhou/p/3965113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!