//实例对象的方法,以下分别有
//objproto.isPrototypeOf(obj) objproto在obj的原型链上吗
//obj.propertyIsEnumerable(prop) obj的prop属性是否可枚举
//自定义属性默认可枚举,内置对象及属性默认不可枚举
//原型链上的属性不可枚举
//obj.hasOwnProperty(prop) 包括可枚举、不可枚举属性
//obj.toString() 可用于类型检测
//obj.valueOf() 返回原始值,arr、obj、fn返回自身
//prototypeObj.isPrototypeOf(obj)
function Foo() {}
function Bar() {}
function Baz() {}
Bar.prototype = Object.create(Foo.prototype)
Baz.prototype = Object.create(Bar.prototype)
let baz = new Baz()
//原型链 实例baz -> Baz.prototype -> Bar.prototype -> Foo.prototype -> Function.prototype -> Object.prototype
console.log(Baz.prototype.isPrototypeOf(baz))
console.log(Bar.prototype.isPrototypeOf(baz))
console.log(Foo.prototype.isPrototypeOf(baz))
console.log(Object.prototype.isPrototypeOf(baz))
//obj.propertyIsEnumerable(prop)
let o = {}
let a = []
o.prop = 'is enumerable'
a[0] = 'is enumerable'
console.log('-------------')
console.log(o.propertyIsEnumerable('prop'))
console.log(a.propertyIsEnumerable(0))
//自定义、内置对象的可枚举性
//自定义属性默认可枚举
//内置对象及属性默认不可枚举
let arr = ['is enumerable']
console.log(arr.propertyIsEnumerable(0)) //true
console.log(arr.propertyIsEnumerable('length')) //false
console.log(Math.propertyIsEnumerable('random')) //false
console.log(Math.propertyIsEnumerable('PI')) //false
console.log(this.propertyIsEnumerable('Math')) //false
//自身属性和继承属性
//自身属性可枚举
//继承属性不可枚举
let arr1 = []
arr1.propertyIsEnumerable('constructor') //false
function firstConstructor() {
this.property = 'is not enumerable'
}
firstConstructor.prototype.firstMethod = function() {}
function secondConstructor() {
this.method = function method () {return 'is enumerable'}
}
secondConstructor.prototype = new firstConstructor()
secondConstructor.prototype.constructor = secondConstructor
let obj = new secondConstructor()
obj.arbitraryProperty = 'is enumerable'
console.log('-------------')
console.log(obj.propertyIsEnumerable('arbitraryProperty')) //true
console.log(obj.propertyIsEnumerable('method')) //true
console.log(obj.propertyIsEnumerable('property')) //false 在secondConstructor的原型上
console.log(obj.propertyIsEnumerable('firstMethod')) //false 在firstConstructor的原型上
//原型链 obj -> secondConstructor.prototype -> firstConstructor.prototype -> Object.prototype
//构造函数的原型都是由Object创建的
console.log(secondConstructor.prototype.isPrototypeOf(obj)) //true
console.log(firstConstructor.prototype.isPrototypeOf(obj)) //true
console.log(Object.prototype.isPrototypeOf(obj)) //true
console.log(firstConstructor.prototype) //由Object创建的原型
console.log(Function.prototype === Function.__proto__) //true
//obj.hasOwnProperty(prop)
let obj1 = new Object()
obj1.propOne = null
console.log('------------')
console.log(obj1.hasOwnProperty('propOne')) //true
obj1.propTwo = undefined
console.log(obj1.hasOwnProperty('propTwo')) //true
obj1.prop = 'exist'
console.log(obj1.hasOwnProperty('prop')) //true
//自身属性、继承属性
console.log(obj1.hasOwnProperty('toString')) //false
console.log(obj1.hasOwnProperty('valueOf')) //false
//遍历所有自身属性
let buz = {
fog: 'stack'
}
for (let name in buz) {
if (buz.hasOwnProperty(name)) {
console.log('this is (' + name + ') for sure. Value: ' + buz[name])
} else {
console.log(name)
}
}
//hasOwnProperty作为属性名被占用
let foo = {
hasOwnProperty: function() {
return false
},
bar: 'Here be dragons'
}
console.log(foo.hasOwnProperty('bar')) //false
console.log(({}).hasOwnProperty.call(foo,'bar')) //true
console.log(Object.prototype.hasOwnProperty.call(foo,'bar')) //true
//所有继承了Object的对象,都拥有这个方法
let str = new String('str')
console.log(str.hasOwnProperty('length')) //true
let arr2 = new Array()
console.log(arr2.hasOwnProperty('length')) //true
console.log(Math.hasOwnProperty('PI')) //true
console.log(str) //原型链 str -> String.prototype -> Object.prototype -> null
//obj.toString()
let obj3 = new Object()
console.log('--------------')
console.log(obj3.toString()) //[object Object]
let obj4 = {name:'zhangsan'}
console.log(obj4.toString()) //[object Object]
//toString()检测对象类型
let toString = Object.prototype.toString
console.log(toString.call(new Date)) //[object Date]
console.log(toString.call(new String)) //[object String]
console.log(toString.call(Math)) //[object Math]
console.log(toString.call(undefined)) //[object Undefined]
console.log(toString.call(null)) //[object Null]
//obj.valueOf() 返回对象的原始值primitiveValue
//arr.obj.fn 返回值是本身
let array = ['a','b',33]
console.log('--------------')
console.log(array.valueOf() === array) //true
let date = new Date()
console.log(date.valueOf()) //1970-1-1到此刻的总毫秒数
let num = 15.456
console.log(num.valueOf())
let str1 = '123456abc'
console.log(str1.valueOf()) //123456abc
let bool = true
console.log(bool.valueOf()) //true
let newBool = new Boolean(true)
console.log(newBool.valueOf() === newBool) //false
let fun = function () {}
console.log(fun.valueOf() === fun) //true
let obj5 = {}
console.log(obj5.valueOf() === obj5) //true obj,arr,fn的返回值是自身
对象实例obj的常用方法及原型链
最新推荐文章于 2023-12-12 17:21:27 发布