面向对象2

2018-09-25  本文已影响0人  追逐_e6cf

一、原型

//Person.prototype  //原型
//Person.prototype = {}  //祖先

//Person.prototype.name = "zhangsan";
Person.prototype = {
  name : "zhangsan"
}
Person.prototype.say = function(){
  console.log("I am saying");
}
function Person(){
  this.name = "lisi";
}
var person = new Person();
//先找Person,Person没有再去原型上去找
Person.prototype.name = "zhangsan";
function Person(){
  //var this = {
  //  __proto__:
  //}
}
Person.Prototype = {
  name : "lisi"
}
var person = new Person();

//Person.prototype = {name:"zhangsan"};
//__proto__ = Person.prototype;
//Person.prototype = {name:"lisi"};

//var obj = {name:1};
//var obj1 = obj;
//obj = {name:2};

二、原型链

Person.prototype = {
  weight : 100
}
function Person(){
  this.eat = function(){
    this.weight ++;
  }
}
var person = new Person();
//person();  原型上的weight不会改变,但是person自身会增加一个weight属性

One.prototype.skill = "C++";
function zhangsan(){

}
var one= new One();
Two.prototype = one;
function Two(){
  this.p_skill = "java";
}
var two = new Two();
Three.prototype = two;
function Three(){
  this.s = "javascript"
}
var three = new Three();
var obj = {};
var o = {name:"abc", age:24}
var obj = Object.create(null);
obj.toString = function(){
  return "123"
}
document.write(obj);

call、apply

function Person(name, age){
  this.name = name;
  this.age = age;
}
var person = new Person("zhansan", 24);
var obj = {};
Person.call(obj, "lisi", 24);

function Person(name, age, sex){
  this.name = name;
  this.age = age;
  this.sex = sex;
}
function Person1(name, age, sex, height){
  Person.apply(this, [name, age, sex]);
  //Person.call(this, name, age, sex);
  this.height = height;
}
var person1 = new Person1("zhangsan", "24", "男", 170);
上一篇 下一篇

猜你喜欢

热点阅读