JS对象和原型以及构造函数

function person(firstname,lastname,age,eyecolor)
   {
    this.firstname=firstname;
    this.lastname=lastname;
    this.age=age;
    this.eyecolor=eyecolor;

    this.changeName=cN;
    function cN(name)
    {
        this.lastname=name;
    }
   }

构造函数

var stooge = new person();//stooge被构造成一个崭新的对象(即原型)

————————————————————————————————————————————————————

if(typeof Object.beget !== 'function')
{
Object.beget = function(o)
{
           var F = function(){};
              F.prototype = o;
               return new F();
}
}

创建新对象的函数

another_stooge = Object.beget(stooge);//another_stooge是基于stooge这个原型创造
   出来的对象;每个对象都连接到一个原型对象,并且继承原型对象的属性。
——————————————————————————————————————————————
stooge.hobby = "games";

console.log(another_stooge.hobby)  // games


原型新添加的属性会出现在其它基于此原型的对象上

___________________________________________________________________________________________________________

对象和数组是引用传值

var a = ["q","w","e","r"];
var b = a;
    b.pop();
    console.log(b);

    console.log(a);



猜你喜欢

转载自blog.csdn.net/liuanpingfirst/article/details/80616276
今日推荐