日期:2014-05-16  浏览次数:20375 次

这里的this到底是指向哪里的.高手进来
console.log(this == Object)
onsole.log(this == obj)
console.log(this == a)
以上三个全是false.
而且this.initialize.apply(this,arguments); 这里这样写到底有什么区别或者有什么好处啊?一直想不明白

JScript code
var obj = {
                create : function(){
                    return function(){
                        console.log(this == Object)
                        console.log(this == obj)
                        console.log(this == a)
                        this.initialize.apply(this,arguments);
                    }
                }
           }
           var a = obj.create();
            a.prototype = {  
                initialize :function(v) {
                    this.name = v;
                },
                bb : function(){
                    return this.name;
                }
           }
           var aa = new a("sd");
           alert(aa.bb())


------解决方案--------------------
这个问题深奥了 我也是一知半解 只是说说自己见解吧
如果 把 var aa = new a("sd"); 改为 a("sd");
那可以肯定的是 this==window 这个不用怀疑吧 因为a 有全局对象window来调用 谁调用this指向谁 create的 this==obj也不用说了 
而 new function 的作用是 引用下专业语言 
A new object is created, inheriting from foo.prototype. 
The constructor function foo is called with the specified arguments and this bound to the newly created object. new foo is equivalent to new foo(), i.e. if no argument list is specified, foo is called without arguments. 
The object returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.) 
差不多的意思是
1 创建一个新的对象,并让 this 指针指向它
2 将函数的 prototype 对象的所有成员都赋给这个新对象
3 执行函数体 对这个对象进行初始化操作
4 返回1中创建的对象
所以这里的this指的是a的一个对象 这是javascript规定的对象 有自己的方法 可以log出来看看
不等于任何一个其他对象哦