{}的原型对象
偶然之间看见了一个空对象(不是Object哦)的Prototype,就想看看他的Prototype对象都有什么。
console.log({});
PS:这里额外说一嘴:__proto__就是空对象的原型链,里面就是Object的prototype的属性。(准备另起一文,去整理整理Object的属性方法)
okk,接下来一个个查下文档吧⬇️
1、constructor
构造函数,所有对象都会从它的原型上继承一个constructor 属性
可以发现,这里面的方法都是和Object相关的方法,这里的空对象继承自Object
那数组和其他类型是不是也有继承自什么啊?⬇️⬇️⬇️
var o = {};
o.constructor === Object; // true
var o = new Object;
o.constructor === Object; // true
var a = [];
a.constructor === Array; // true
var a = new Array;
a.constructor === Array // true
var n = new Number(3);
n.constructor === Number; // true
****总结:
1、分两个概念,实例对象,也就是空对象{},JS的类Object。
2、{}的__proto__就是Object的prototype(上文有提到,这里再说一遍)。
3、{}有一个属性constructor中文名字叫构造函数,是从它的原型Object上继承过来的,这个继承过来的constructor里面的属性和方法和Object里面的属性方法都是一样的。
4、于是乎Object的prototype的属性的constructor属性的prototype方法的constructor属性—>无限套娃
5、很乱,自己的理解,慢慢琢磨吧!
2、hasOwnProperty
指定对象是否具有指定属性,返回布尔值
let obj = {
name:"obj"
}
console.log(obj.hasOwnProperty("name")); // true
console.log(obj.hasOwnProperty("age"));// false
3、isPrototypeOf
一个对象是否在另一个对象的原型链上,返回布尔值
console.log(Object.prototype.isPrototypeOf({})); // true
console.log(Array.prototype.isPrototypeOf([])); // true
function Fn1(){}
let fn1 =new Fn1()
console.log(Fn1.prototype.isPrototypeOf(fn1)); //true
Object的prototype方法(是个对象)在空对象{}的原型链上
Array的prototype方法(是个对象)在空数组[]的原型链上
函数Fn1的prototype方法(是个对象)在对象fn1的原型链上,因为fn1是函数Fn1的实例
4、propertyIsEnumerable
指定对象的指定属性是否是枚举类型,返回布尔值
let obj = {
name:"obj",
enum: {
orange : 1,
banana : 2,
peach : 3,
strawberry : 4
}
}
console.log(obj.propertyIsEnumerable("name")); // true
console.log(obj.propertyIsEnumerable("enum")); // true
console.log(obj.propertyIsEnumerable("age")); // false
可以理解成指定属性可否通过
for..in..
获取到,能获取到就是ture。枚举这个概念,emmm…
5、toLocaleString、toString
返回一个表示该对象的字符串,toLocaleString 返回调用 toString() 的结果
console.log({}.toString()); //[object Object]
console.log({}.toLocaleString()); //[object Object]
function Dog(name) {
this.name = name;
}
const dog = new Dog('Gabby');
Dog.prototype.toString = function dogToString() {
return `${this.name}`;
};
console.log(dog.toString()); //Gabby
console.log(dog.toLocaleString()); //Gabby
6、valueOf
返回指定对象的原始值
// Array:返回数组对象本身
var array = ["ABC", true, 12, -5];
console.log(array.valueOf() === array); // true
// Date:时间距1970年1月1日午夜的毫秒数
var date = new Date(2013, 7, 18, 23, 11, 59, 230);
console.log(date.valueOf()); // 1376838719230
// Number:返回数字值
var num = 15.26540;
console.log(num.valueOf()); // 15.2654
// 布尔:返回布尔值true或false
var bool = true;
console.log(bool.valueOf() === bool); // true
// new一个Boolean对象
var newBool = new Boolean(true);
// valueOf()返回的是true,两者的值相等
console.log(newBool.valueOf() == newBool); // true
// 但是不全等,两者类型不相等,前者是boolean类型,后者是object类型
console.log(newBool.valueOf() === newBool); // false
// Function:返回函数本身
function foo(){}
console.log( foo.valueOf() === foo ); // true
var foo2 = new Function("x", "y", "return x + y;");
console.log( foo2.valueOf() );
/*
ƒ anonymous(x,y
) {
return x + y;
}
*/
// Object:返回对象本身
var obj = {name: "张三", age: 18};
console.log( obj.valueOf() === obj ); // true
// String:返回字符串值
var str = "http://www.xyz.com";
console.log( str.valueOf() === str ); // true
// new一个字符串对象
var str2 = new String("http://www.xyz.com");
// 两者的值相等,但不全等,因为类型不同,前者为string类型,后者为object类型
console.log( str2.valueOf() === str2 ); // false
对象 | 返回值 |
---|---|
Array | 返回数组对象本身。 |
Boolean | 布尔值。 |
Date | 存储的时间是从 1970 年 1 月 1 日午夜开始计的毫秒数 UTC。 |
Function | 函数本身。 |
Number | 数字值。 |
Object | 对象本身。这是默认情况。 |
String | 字符串值。 |
– | Math 和 Error 对象没有 valueOf 方法。 |
7、defineGetter、defineSetter、lookupGetter、lookupSetter
Web标准已废弃,不要用了。