谢谢我觉得这样解释可能更清楚些test作为一个函数对象,分为内外两部分,内的就是{}中间的部分,其余的都是其外边的部分包括prototype也是外边的部分当我们使用 var t = new test();时,首先是创建了一个新的空的对象,然后将这个空的对象作为test的this调用,这样t就拥有了test内的所有东东,然后将t的隐式链指向test的prototype,这样又拥有了test.prototype上的东东但是,test外边的东西,除了test.prototype外,其余的东东都没有付给这个t对象,所以,t无法访问到,test外的除test.prototype外的东东
JS定义对象有N中方式,随便给一种:var obj = new Object(); obj.name=111; obj.age="122"; obj.say=new function(){}
prototype属性封装了对象的全部属性和方法,继承我上面写的对象: var obj = new Object(); obj.name=111; obj.age="122"; obj.say=new function(){}var obj2 = obj.prototype; alert(obj2.name);
test.prototype.alert=function(){alert("");};
<script> function test(){ this.name="ddd"; } test.age = 1; test.prototype.age1 = 2; var t = new test(); test.prototype.age2 = 3; t.age3=4; test.prototype.age4 = 5; alert("t.age:"+t.age); alert("t.age1:"+t.age1); alert("t.age2:"+t.age2); alert("t.age3:"+t.age3); alert("t.age4:"+t.age4); alert("test.age:"+test.age); alert("test.age1:"+test.age1); alert("test.age2:"+test.age2); </script> 这个例子,很说明问题。 看一下结果,就都明白了。
function test(){ this.name="ddd"; } test.age = 23; test.prototype.age1 = 34;可以直接 test.age 使用,很像.net 里的 public class test { public static int age=23; }
function test(){ this.name="ddd";//这样相当于实例中的变量 } test.age = 23;//这样写相当于静态变量
对了还有constructor这东东,视乎很邪恶有高手能解释下自己的理解没?
function 是Function的实例,和new function()得到的对象没有关系。function有prototype属性,new function()时会把prototype这个对象复制过来作为基础,构建一个新对象。这个新对象和prototype已经没有关系。只是instanceof会指向prototype对象。new function()时,这个function本身就是一个constructor。
function test(){
this.name="ddd";
}
test.age = 23;
test.prototype.age1 = 34;
var t = new test();
alert(t.name+":"+t.age);//t不是test对象,t的类型为test,跟var str = "ddddd";这里str的类型为string
//所以这里t.age是取不到test的属性的
//prototype是定义原型,即类型为test的对象都可以调用
alert(t.name+":"+t.age1);
alert(t.name+":"+test.age);</script>
这样说LZ应该清楚了吧
谢谢我觉得这样解释可能更清楚些test作为一个函数对象,分为内外两部分,内的就是{}中间的部分,其余的都是其外边的部分包括prototype也是外边的部分当我们使用 var t = new test();时,首先是创建了一个新的空的对象,然后将这个空的对象作为test的this调用,这样t就拥有了test内的所有东东,然后将t的隐式链指向test的prototype,这样又拥有了test.prototype上的东东但是,test外边的东西,除了test.prototype外,其余的东东都没有付给这个t对象,所以,t无法访问到,test外的除test.prototype外的东东
obj.name=111;
obj.age="122";
obj.say=new function(){}
var obj = new Object();
obj.name=111;
obj.age="122";
obj.say=new function(){}var obj2 = obj.prototype;
alert(obj2.name);
function test(){
this.name="ddd";
}
test.age = 1;
test.prototype.age1 = 2;
var t = new test();
test.prototype.age2 = 3;
t.age3=4;
test.prototype.age4 = 5;
alert("t.age:"+t.age);
alert("t.age1:"+t.age1);
alert("t.age2:"+t.age2);
alert("t.age3:"+t.age3);
alert("t.age4:"+t.age4);
alert("test.age:"+test.age);
alert("test.age1:"+test.age1);
alert("test.age2:"+test.age2);
</script>
这个例子,很说明问题。
看一下结果,就都明白了。
更本质的理解(1)看ECMA-262规范 (2)看JS引擎代码其实LZ前面的理解还是不错的,可以理解为对象在JS中的引擎内存中的表达方式(我也没有看过代码,纯粹是自己猜想)
Object {
construnctor
this(IE似乎直接创建原型链的引用) // 实例化后产生的方法,通过this引用
__PROTO__(指向原型链,IE不支持)
其它函数 // 也可以对象OO中的静态函数,必须通过Object.方式才能引用。
}
this.name="ddd";
}
test.age = 23;
test.prototype.age1 = 34;可以直接 test.age 使用,很像.net 里的
public class test
{
public static int age=23;
}
this.name="ddd";//这样相当于实例中的变量
}
test.age = 23;//这样写相当于静态变量