//对象名只是某个对象的引用,并不是副本。
(function() {
function a() {
var k = {a : 1};
var kk = 1;
b(k,kk);
console.log(k); //{a:2}
console.log(kk); //1
}
function b(k,kk) {
k.a = k.a + 1;
kk = 2;
}
a();
var obj1 = {},
obj2 = {};
var obj3 = obj4 = {};
obj1 === obj2 && console.log("obj1 === obj2");
obj1 == obj2 && console.log("obj1 == obj2");
obj3 === obj4 && console.log("obj3 === obj4"); //obj3 === obj4
})();
(function () {
function kk() {
var val = 1;
var obj = {x:1};
var arr = [1,2];
function c() {
return val + 1;
}
return {
a : function() {
console.log(val);
},
b : function() {
this.a();
console.log(c());
},
c : function () {
return obj;
},
d : function () {
return val;
},
f : function () {
return arr;
}
};
};
var k1 = kk();
k1.b();
var o = k1.c(); //对obj的另一个引用
console.log(o); //{x:1}
o.x = 3; //对其修改相当对所引用的对象进行修改
console.log(k1.c()); //{x:3}
var v = k1.d(); //创建了val的副本
console.log(v); //1
v = 9; //副本的改变对val不影响
console.log(k1.d()); //1
var a = k1.f();
console.log(a); //[1,2]
a[1] = 9;
console.log(k1.f()); //[1,9]
a = [2,3]; //引用了另一个对象,对原来的引用不影响
console.log(k1.f()); //[1,9]
})();
//对象名只是某个对象的引用,并不是副本。(function() { function a() { var k = {a : 1}; var kk = 1; b(k,kk); console.log(k); //{a:2} console.log(kk); //1 } function b(k,kk) { k.a = k.a + 1; kk = 2; } a