<body>
<script>
var d = null;
function create(elm,parent,fn,o){
o = document.createElement(elm);
fn&&fn(o);
parent&&parent.appendChild(o);
return o
};
create('div',null,function(elm){elm.id='ss'},d)
alert(d); //d还是null 我以为是obj了
</script>
</body>
<script>
var d = null;
function create(elm,parent,fn,o){
o = document.createElement(elm);
fn&&fn(o);
parent&&parent.appendChild(o);
return o
};
create('div',null,function(elm){elm.id='ss'},d)
alert(d); //d还是null 我以为是obj了
</script>
</body>
你d=null
然后给null赋值当然没用了啊
相当于
null = 2
看错了<script>
var d = null ;
function create(elm,parent,fn,o){
o = document.createElement(elm);
fn&&fn(o);
parent&&parent.appendChild(o);
return o
};
d = create('div',null,function(elm){elm.id='ss'},d)
alert(d);
</script>
<body>
<script>
var d =1;
function create(o){
o = 2;
return o
};
create(d)
alert(d); //d输出还是1
</script>
</body>
不过确切的说变量o是变量d的引用,除非直接对这个引用操作,才会改变d的值,
例子:var a=[]; (function(o){o.push(2);})(a); alert(a);
如果改变o的引用(如赋值),o的作用域只在create