【 在 zzjyingzi (十六点五) 的大作中提到: 】
: 理解javascript是基于对象的,这个就不难理解。
: 因为基于对象,所以才有原型链,变量、方法是挂接在对象上的,在方法中的直接体现就是this的指向,函数默认this指向的对象在浏览器中是window,window默认挂接全局变量和方法,而prototype就是指向对象的API,这样可以将函数的指向对象更换为其他对象,比如可以
: var a = 1;
: function asd(){
: alert(this.a);
: }
: asd();//这时候this指向window
only applies to top-level function calls.
function calls in another function without a base ref
has `this' set to undefined.
: asd.prototype.s = function(){
: this.a = 2;
: alert(this.a);
: };
: var z = new asd(); //构造之后this就指向新的作用域,这时候函数的this为undefined
: z.s(); //这时候是2
--
FROM 202.120.55.*