美文网首页
2 js之prototype

2 js之prototype

作者: 一枝妖孽 | 来源:发表于2018-05-28 21:56 被阅读0次

原型

  • prototype是function中的一个属性,也是一个对象
  • prototype是一个json格式的对象,可以动态的往json中添加一些内容
    Person.prototype.name = function(){
    alert("name");
    }
    Person.prototype["sex"] = "male";
  • 根据构造器可以创建对象,而创建出来的对象就拥有了prototype中的数据
    /**
  • 1 在一个对象中有一个属性:prototype
  • 2 prototype是一个对象
    */
function Person(){
    
}
Person.prototype;

Person.prototype.name=function(){
    alert("name");
}
Person.prototype.age=5;
Person.prototype['sex']="male";


window.onload=function(){
    Person.prototype;
}

/**
 * p对象就具有 Person的 属性了
 */
var p=new Person();
alert(p.age);
alert(Person.age);//undefined

function SupPerson(){
    
}
/**
 * 让SupPerson创建出来的对象拥有Person原型中的内容
 */
//方式一
//SupPerson.prototype=Person.prototype;
//方式二
SupPerson.prototype=p;
var sp=new SupPerson();
alert(sp.age);

相关文章

网友评论

      本文标题:2 js之prototype

      本文链接:https://www.haomeiwen.com/subject/icenjftx.html