1. 内置对象数组
const a = '123'; // Output: 123 输出值类型 123,是基本数据类型
const b = new String('123'); // Output: String {'123'} 输出对象类型, 是复杂数据类型
Object.getPrototypeOf(a) === Object.getPrototypeOf(b); // true, a和b的原型都是同样的,都指向String内置对象
复制代码
const s1 = 'jiaxin';
const s2 = s1.substring(2);
复制代码
const s1 = new String('jiaxin');
const s2 = s1.substring(2);
s1 = null;
复制代码
1. 语法bash
new Object(a) // a可为数据类型,如 number, string, Object函数
2. 特征测试
// 如下建立的二者是同样的
new Object(1);
new Number(1);
复制代码
// 再举个栗子, 也是一致的
new Object('111');
new String('111');
复制代码
new Object(null);
new Object(undefined);
new Object({
name: 'jiaxin',
age: 18
});
复制代码
// instanceof运算符用于测试构造函数的prototype属性是否出如今对象的原型链中的任何位置
new Object('foo') instanceof String; // true
new Object(true) instanceof Boolean; // true
复制代码
function Foo () {};
new Object(Foo) === Foo; // true 若是Object的参数是一个对象,则返回原对象
复制代码
1. 语法ui
Object.create(proto, [propertiesObject])this
const Girl = {
name: 'jiaxin',
age: 18,
sayHello: function() {
console.log(`Hello, I am ${this.name}`);
}
}
let girl1 = Object.create(Girl); // Output: {}
girl1.__proto__ === Girl; // true 建立出来的对象的原型指向其指定的对象Girl
复制代码
2. 实现原理spa
function Foo() {};
const foo = Object.create(Foo);
const fn = function() {};
fn.prototype = Foo;
const foo = new fn()
复制代码
3. 用new Object()实现Object.create()prototype
function Foo() {};
const fn = Object.create(Foo.prototype);
// 等价于
const fn = new Object();
fn.__proto__ = Foo.prototype;
复制代码