原型插件 prototype

function Person(opts) {
  var def = {
    name: '何XX',
    age: 10,
    sex: '男'
  };
  opts = $.extend(def,opts);
  this.name = opts.name;
  this.age = opts.age;
  this.sex = opts.sex;
}
Person.prototype.output= function () {
  console.log(this.name);
};


//调用方法1:
var tom = new Person({   name: "大叔",   age: 2009,   sex: '女' });
tom.output();

//调用方法2:
var o = new Object();
Car.call(o, "Dudu", 2010, 5000);
console.log(o.output());

 

 

 

//http://segmentfault.com/a/1190000002701241

 function CreateCar(name, color) {
        this.name = name;
        this.color = color;
    }

    CreateCar.prototype.material = function() {
        //绑定material到函数原型链上,当实例化时这个函数会作为每个对象的构造函数
        alert(this.name);
    }

    var car1 = new CreateCar('BMW', '白色');
    var car2 = new CreateCar('Benz', '黑色');

    console.log(car1);
    console.log(car2);
    console.log(car1.hasOwnProperty("material"));//false 因为material是原型链上的,不是对象本身的,所以false

 

 

 

posted @ 2015-01-30 15:21  幹掉上帝  阅读(225)  评论(0编辑  收藏  举报