个人记录,有错误恳求告知
对象的新增方法
- Object.is(a,b) 判断两个值是否相等
==(相等运算符) ===(严格相等运算符)前者会自动转换数据类型,后者的NaN不等于自身 +0 和 -0相等
- Object.assign(target,source1,source2) 是将目标对象的可枚举属性复制到源对象上
- 只有字符串的包装对象,才会产生可枚举属性 注意点
- 浅拷贝
- 同名属性的替换
- 数组的处理 将数组当成对象来处理
- 取值函数的处理 只能进行值的复制,如果为取值函数。那么取值后进行处理
proto 属性
- 其本质上不是一个对外暴漏的API
- 目前推荐使用Object.setPrototypeOf(),Object.getPrototypeOf(),Object.create()
- proto 调用的是Object.prorotype.proto
- Object.setPrototypeOf(obj,proto)
function Rectangle() {
// ...
}
const rec = new Rectangle();
Object.getPrototypeOf(rec) === Rectangle.prototype
// true
源码实现
Object.defineProperty(Object.prototype, '__proto__', {
get() {
let _thisObj = Object(this);
return Object.getPrototypeOf(_thisObj);
},
set(proto) {
if (this === undefined || this === null) {
throw new TypeError();
}
if (!isObject(this)) {
return undefined;
}
if (!isObject(proto)) {
return undefined;
}
let status = Reflect.setPrototypeOf(this, proto);
if (!status) {
throw new TypeError();
}
},
});
function isObject(value) {
return Object(value) === value;
}
JS中创建对象的N种方式
- 创建一个Object的对象实例而后为其添加属性和方法
var person = new Object();
person.name = "zhang"
person.sayname = function(){
alert(this.name)
}
- 对象字面量
var person = {
name: "zhang"
sayname: function(){
}
}
- 工厂函数
function createperson(name,age,job){
var o = new Object();
o.name = name
o.sayname = function(){
}
return o
}
var person1 = createperson("zhang",29,"engineer")
console.log(person1 instanceof createperson) false
console.log(person1 instanceof Object) true
- 构造函数
function Person(name,age,job){
this.name = name
this.sayname = function(){
}
等同于
this.sayname = new Function()
}
var person1 = new Person("zhang",29,"engineer")
console.log(person1 instanceof Person) true
console.log(person1 instanceof Object) true
new,所经历的四个过程
- 创建一个新的对象
- 将构造函数的作用域赋给新对象
- 执行构造函数代码
- 返回新对象
构造函数模式中person1.constructor === person
- 原型创建对象模式
function Person(){
}
Person.prototype.name = 'Nike'
Person.prototype.sayName = function(){
}
var person1 = new Person();
6.组合使用构造函数模式和原型模式
function Person(name,age,job){
this.name =name;
this.age = age;
this.job = job;
}
Person.prototype = {
constructor:Person,
sayName: function(){
alert(this.name);
};
}
var person1 = new Person('Nike',20,'teacher');
xdm,学费了
运算符的拓展
没啥可以记的
Symbol
感觉也没啥可以记的
Set 和 Map 数据结构
- Set 集合
- Map Hash表 与对象类似
- 不同的是 Object 结构提供了 字符串-值得对应 Map结构提供了值-值得对应
具体有需要再看吧,不是应用得重点