console.debug(p1.hasOwnProperty(‘name‘));//false 实例中没有name属性console.debug(‘name‘ in p1);//输出true 实例或者原型中有name属性p1.name = ‘Lebron‘;console.debug(p1.hasOwnProperty(‘name‘));//true 实例中有name属性console.debug(‘name‘ in p1);//输出true 实例或者原型中有name属性
function hasPrototypeProperty(object,name){return (name in object) && (!object.hasOwnProperty(name));}console.debug( hasPrototypeProperty(p2,‘name‘));//返回true name属性在原型中p2.name = ‘kobe‘;//实例p2增加name属性console.debug( hasPrototypeProperty(p2,‘name‘));//返回false name属性不在原型中
function ownPrototypeHasProperty(object,name){while( Object.getPrototypeOf(object).constructor != Object){//object的原型的构造函数不为Objectvar pro = Object.getPrototypeOf(object);//获取这个原型//如果原型中有这个属性if(pro.hasOwnProperty(name)){return true;}//顺着原型向上搜索object = pro;}return false;}var v3 = new Person();console.debug( ownPrototypeHasProperty(v3,‘name‘));//true v3的原型中有name属性console.debug( ownPrototypeHasProperty(v3,‘sex‘));//false v3的原型中木有sex属性
console.debug(‘---------------‘);function itratePropertyOf(object){for( var per in object){console.debug(per);}}itratePropertyOf(v3);
function Person() {}Person.prototype.name = "Nicholas";Person.prototype.age = 29;Person.prototype.job = "Software Engineer";Person.prototype.sayName = function() {console.debug(this.name);};var keys = Object.keys(Person.prototype);console.debug(keys); //"name,age,job,sayName"
function Person() {}Person.prototype.name = "Nicholas";Person.prototype.age = 29;Person.prototype.job = "Software Engineer";Person.prototype.sayName = function() {console.debug(this.name);};function Per (){console.debug(‘Per构造函数‘);}//设置per的原型 使用对象字面量的方式,但是这样会使Per的原型失去construtor(会使用Object() ),所以要制定constructor 为 PerPer.prototype = {name : ‘kobe‘,sayName : function(nameStr){this.name = nameStr;},constructor : Per};var p1 = new Per();var p2 = new Per();console.debug(p1);console.debug(p2);
function Person(){}//Person的原型指向新对象Person.prototype = {constructor: Person,name : "Nicholas",age : 29,job : "Software Engineer",sayName : function () {alert(this.name);}};var friend = new Person();Person.prototype.sayHi = function(){alert("hi");};friend.sayHi(); //"hi" works!
function Person(){}var friend = new Person();//Person的原型对象指向了一个新的对象Person.prototype = {constructor: Person,name : "Nicholas",age : 29,job : "Software Engineer",sayName : function () {alert(this.name);}};//friend.sayName(); //Uncaught TypeError: Object #<Person> has no method ‘sayName‘//friend对象的原型指针仍然指向的是旧的Person的原型对象导致错误的发生
/**原型模式不仅仅适用于创建自定义类型,原生的引用类型也是采用原型模式创建的**/console.debug(typeof Array.prototype.sort); //"function"console.debug(typeof String.prototype.substring); //"function"//通过原型对象的原型 不仅仅可以取得默认方法的引用,并且可以修改原声对象的引用//下面的代码给基本包装类型String添加一个名为startsWith()的方法String.prototype.startsWith = function(text) {return this.indexOf(text) == 0;};var msg = "Hello world!";console.debug(msg.startsWith("Hello")); //true
//主人 默认名字为kobevar per_1 = new Person();//Dog构造函数function Dog(){}//Dog原型Dog.prototype = {constuctor : Dog,name : ‘Hachi‘,master : per_1,//主人为per_1 per_1的name默认为KobesayName : function(){console.debug(this.name + ‘ 主人‘ +this.master.name);}};//实例化两只狗狗var d1 = new Dog();var d2 = new Dog();d1.sayName();//Hachi 主人kobed2.sayName();//Hachi 主人kobed1.master.name = ‘Garnett‘;//改变狗狗1的主人d1.sayName();//Hachi 主人Garnettd2.sayName();//Hachi 主人Garnett 逻辑上不应该更改的吧
function Person(){}Person.prototype = {constructor: Person,name : "Nicholas",age : 29,job : "Software Engineer",friends : ["Shelby", "Court"],sayName : function () {alert(this.name);}};var person1 = new Person();var person2 = new Person();person1.friends.push("Van");alert(person1.friends); //"Shelby,Court,Van"alert(person2.friends); //"Shelby,Court,Van"alert(person1.friends === person2.friends); //true
function Person(name, age, job){this.name = name;this.age = age;this.job = job;this.friends = ["Shelby", "Court"];}Person.prototype = {constructor: Person,sayName : function () {console(this.name);}};var person1 = new Person("Nicholas", 29, "Software Engineer");var person2 = new Person("Greg", 27, "Doctor");person1.friends.push("Van");console(person1.friends); //"Shelby,Court,Van"console(person2.friends); //"Shelby,Court"console(person1.friends === person2.friends); //falseconsole(person1.sayName === person2.sayName); //true
原文:http://www.cnblogs.com/zhaobing/p/3829960.html