1 2 3 4 5 6 7 8 9 10 11 12 13 |
function obj1(name,age){ this.name=name; this.age=age; this.identity=function(){ var li=document.createElement("p"); var txt=document.createTextNode("白小纯"); li.appendChild(txt); document.body.appendChild(li); } } var person2=new obj1('白小纯',123); person2.identity(); alert(person2.name); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function obj2(){ var lio=new Object(); //创建对象,对象属性赋值 lio.name='lio'; lio.attr='男'; lio.identity=function(){ var li=document.createElement("p"); var txt=document.createTextNode("白小纯"); li.appendChild(txt); document.body.appendChild(li); }; return lio; } var person=obj2(); //alert(person.name); |
1 2 3 4 5 6 7 8 9 10 11 12 |
function obj3(){ //this.name='lio'; } obj3.prototype.name='lio'; obj3.prototype.identity= function (name) { alert("实际上是"+name); }; var person3=new obj3(); //检测是在实例中还是在原型中 alert(person3.hasOwnProperty("name")); alert(person3.hasOwnProperty("rename")); person3.identity('白小纯'); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function obj4(age) { this.age=age; this.rename='aaaa'; }; obj4.prototype={ constructor:obj4, name:'lio', age:123, identity: function (name) { alert(name+"实际上是白小纯"); } }; var person4=new obj4(18); alert(person4.hasOwnProperty("age"));//true person4.identity('lio'); |
from:https://blog.csdn.net/Amouzy/article/details/125974435