console.log([1, 2, 3] === [1, 2, 3]); // false
指向的是同一个地址
var obj1 = {
a: 1,
b: 2
}
var obj2 = obj1;
obj1.a = 'aaa';
obj2.b = 'bbb';
console.log(obj1, obj2);
var arr1 = [1, 2, 3];
var arr2 = arr1;
console.log(arr1 === arr2); //true
var obj1 = {
a: 'hello'
}
var obj2 = obj1;
obj2.a = 'world';
console.log(obj1);
(function () {
console.log(a);
var a = 1;
})()
var o = {
b: 'bbb'
}
var obj1 = {
a: 1,
'张三': '你好'
}
obj1[o] = '123';
for (var k in obj1) {
console.log(typeof k); // 输出结果全部是string
}
var a = {};
var b = {
key: 'b'
}
var c = {
key: 'c'
}
a[b] = '123';
a[c] = '456';
console.log(a[b]); // 456
// 这个问题类似于
// let m = {};
// m.a = 1;
// m.a = 2;
// console.log(m.a); // 2
1. 每一个函数都自带一个prototype【原型】
注意:对象拥有__proto__
2. new Fun 该Fun构造函数的原型指向于对象(new Fun)的原型
function Fun() { };
let obj = new Fun();
console.log(Fun.prototype == obj.__proto__); //true
function Fun() {
this.a = '在Fun函数中添加的';
}
Fun.prototype.a = '在Fun原型添加的';
let obj = new Fun();
obj.a = '对象本身';
obj.__proto__.a = '这是对象原型添加的';
Object.prototype.a = '这是Object添加的';
console.log(obj.a); // 对象本身
function Fun() {
this.a = '在Fun函数中添加的';
}
Fun.prototype.a = '在Fun原型添加的';
let obj = new Fun();
// obj.a = '对象本身';
obj.__proto__.a = '这是对象原型添加的';
Object.prototype.a = '这是Object添加的';
console.log(obj.a); // 在Fun函数中添加的
function Fun() {
// this.a = '在Fun函数中添加的';
}
Fun.prototype.a = '在Fun原型添加的';
let obj = new Fun();
// obj.a = '对象本身';
obj.__proto__.a = '这是对象原型添加的';
Object.prototype.a = '这是Object添加的';
console.log(obj.a); // 这是对象原型添加的
function Fun() {
// this.a = '在Fun函数中添加的';
}
Fun.prototype.a = '在Fun原型添加的';
let obj = new Fun();
// obj.a = '对象本身';
// obj.__proto__.a = '这是对象原型添加的';
Object.prototype.a = '这是Object添加的';
console.log(obj.a); // 在Fun原型添加的
function Fun() {
// this.a = '在Fun函数中添加的';
}
// Fun.prototype.a = '在Fun原型添加的';
let obj = new Fun();
// obj.a = '对象本身';
// obj.__proto__.a = '这是对象原型添加的';
Object.prototype.a = '这是Object添加的';
console.log(obj.a); // 这是Object添加的