menu
more_vert
js面向对象写法

作者:dayu

//对象字面量写法
var fn = {
    name: ‘hello world‘,
    fn1: function() {
        console.log(this.name);
    }
};
fn.fn1();
//prototype原型写法
function Fn() {
    this.name = ‘hello world‘;
    this.fn1 = function() {
        console.log(this.name);
    };
    Fn.prototype.fn2 = function() {
        console.log(this.name);
    };
}
Fn.prototype.fn3 = function() {
   console.log(this.name);
};
Fn.prototype.init = function() {
    fn.fn1();
    fn.fn2();
    fn.fn3();
};
var fn = new Fn();
fn.init();

  

js面向对象写法

原文地址:http://www.cnblogs.com/huangtonghui/p/7221435.html