2012-04-26 118 views
3
if(!sky.containers) sky.containers = 
{ 
     Window : function() 
     { 
       this.element = document.createElement("div"); 
       this.element.modal = false; 
       this.element.height = 240; 
       this.element.draggable = true; 
       this.element.resizable = true; 
       this.element.position = "center"; 
       this.element.width = 240; 
       this.element.target = document.body; 
       this.element.title =""; 
       this.element.headerHeight = 30;; 
       this.element.effects = {}; 
       this.element.show = function() 





       return this.element; 

     }} 

在这种情况下关键字THIS是什么?“sky.containers”或“Window”?什么是ELEMENT,如果没有变量定义这个名字?Javascript关键字THIS和ELEMENT

+0

试着做一个console.log(this)或alert(this)来获得更多关于'this'的细节。 – 2012-04-26 17:53:30

+0

其说:[object,object] – ozsenegal 2012-04-26 17:54:12

+0

不要使用alert,请使用console.log,特别是在Chrome或FireFox(带FireBug)时,它会给出更详细的输出。警报信息较少。在这种情况下,您可以在这些浏览器中看到“控制台”,这个浏览器带有F12 – Matt 2012-04-26 17:55:32

回答

4

Window()是一个构造函数。这意味着,当你创建一个新的对象,具有类似

var myWin = new Window(); 

在函数内部,this将把刚刚创建的新对象时,它被调用。 (在上面的示例调用中分配给myWin)。

对于'element',它是新创建对象的属性。它不存在,直到这条线:

this.element = document.createElement("div"); 

这将创建一个新的< div>元素和它的DOM表示分配给该属性。

+0

除函数外,返回'this.element'。看起来就像是破坏了其作为构造函数的功能,所以'myWin'将包含新的div元素,而不是Window对象。 – 2012-04-26 17:56:33

+0

“this.element.modal”,“this.element.width”仅适用于模式和宽度是以前创建的DIV DOM元素的属性...对吗? – ozsenegal 2012-04-26 18:04:25

+0

@Michael - no。构造函数的返回值不会影响'new'调用的返回值。 – 2012-04-26 19:24:52

相关问题