构造属性: 在构造函数被创建的时候,在它的原型prototype上有一个contructor属性,是原型对象所独有的。
这是其他对象实例所没有的。
function Person(name,age){this.name = name;this.age = age;}Person.prototype.sayName = function(){console.log(this.name)}var p = new Person("cheng",17)console.log(p.constructor == Person)
1-1 以直接量(对象)的方式定义原型
改变了构造函数的属性
function Person(name, age) {this.name = name;this.age = age;}Person.prototype = {sayName: function () {console.log(this.name)}}var p = new Person("cheng", 17)console.log(p.constructor==Object)
改正构造属性
function Person(name, age) {this.name = name;this.age = age;}Person.prototype = {constructor:Person,sayName: function () {console.log(this.name)}}var p = new Person("cheng", 17)console.log(p.constructor==Object)
1-2 私有和共有属性
console.log(p.hasOwnProperty("name")) //trueconsole.log(p.hasOwnProperty("sayName")) //false
