js object var a =new Object();//定义一个object对象 a.id=10; alert(a.id);//10function 也是一个object对象 function test1(type){ this.type=type; } 所以你可以这样 var a = new test1("122"); alert(a.type);
没有问题 页面的this:在对象中(包括JS对象和页面元素标签对象)时指向该对象 比如<input type="text" value="" onblur="alert(this.value)"> <script> var o = { a:"a", b:"b", c:function (){this.a="c";} }; alert(o.a);//a o.c(); alert(o.a)//c </script> 不在对象中时指向window对象 <script> var a=""; //下面3种写法一样 this.a="aaa"; a="aaa"; window.a="aaa"; </script>
var a =new Object();//定义一个object对象
a.id=10;
alert(a.id);//10function 也是一个object对象
function test1(type){
this.type=type;
}
所以你可以这样
var a = new test1("122");
alert(a.type);
页面的this:在对象中(包括JS对象和页面元素标签对象)时指向该对象
比如<input type="text" value="" onblur="alert(this.value)">
<script>
var o = {
a:"a",
b:"b",
c:function (){this.a="c";}
};
alert(o.a);//a
o.c();
alert(o.a)//c
</script>
不在对象中时指向window对象
<script>
var a="";
//下面3种写法一样
this.a="aaa";
a="aaa";
window.a="aaa";
</script>
var globalType;function test1(type){
gloabType=type;
}function test2(){
alert(gloabType);
}
</script>
在test1中把type属性賦給全局的那个globalType
我現在想起兩個相同的名字,有沒有問題?
我這兩段代碼是否實現相同功能?
如果你是想全局变量的名字一定要用type ,那可以把局部变量的名字改掉呀
比如
<script>
var type;function test1(t){
type=t;
}function test2(){
alert(type);
}
</script>
这样不就不冲突了吗
this.type=type;
}该代码是表示:给test1对象新增的type属性赋上了参数值type。
var globalType; function test1(type){
gloabType=type;
} 该代码中的test1函数:只是给全局变量赋上了参数值type,这与test1对象无关。因此,這兩段代碼實現的功能是不同的,当然test2函数输出的值是相同的。
http://topic.csdn.net/u/20090217/13/653f7949-9e0c-4e99-adb3-dcbc99fca8d5.html