[英]dot notation , window object and its properties
我正在尝试学习有关javascript的一些新概念。这是我编写的简单代码。 在函数内部,THIS关键字指的是作为窗口的全局对象,除非它与另一个对象的上下文绑定。 在myobj
对象内部,有两个方法与另外两个分别称为afunc
和anotherfunc
全局可访问函数共享相同的名称。 我想在myobj上下文中访问那些全局函数,并且当然不使用将全局对象绑定到我用来调用它们的立即调用函数的方式。 但这引发了错误。 我的问题是,如果javascript中的所有内容都是对象,而窗口对象包含它们,那么为什么我可以使用this.afucn
或window.afunc
访问这些功能?
(function(){ var afunc=function (){ document.write('this is world'+'</br>'); } var anotherfunc=function (){ document.write('this is another world'); } var myobj={ afunc:function(){ document.write('afunc'); }, anotherfunc:function(){ document.write('anotherfunc'); }, context:function(){ (function(){ this.afunc(); this.anotherfunc(); })(); } }; myobj.context(); })();
的确,在您的情况下, this
将引用全局对象,而this.afunc()
等将访问全局变量afunc
。
但是,除非要显示更多代码,否则您将没有全局变量afunc
。 您定义的变量
(function(){
var afunc=function (){
document.write('this is world'+'</br>');
}
var anotherfunc=function (){
document.write('this is another world');
}
//...
})();
是封闭函数( (function(){ ... })();
部分) 局部的 。
如果要使它们全局化,可以将它们移到函数外:
var afunc = function() { document.write('this is world' + '</br>'); }; var anotherfunc = function() { document.write('this is another world'); }; (function() { var myobj = { afunc: function() { document.write('afunc'); }, anotherfunc: function() { document.write('anotherfunc'); }, context: function() { (function() { this.afunc(); this.anotherfunc(); })(); } }; myobj.context(); })();
但是 ,请注意,在严格模式下 , this
将是undefined
,不会引用全局对象。 这样做是因为通常如果有人在函数内部使用this
函数,则他们不希望它引用全局对象。 仅通过查看代码,并不总是清楚您是要访问全局对象(通过this
)还是要访问其他对象(并且您已经注意到,您的意图有些混乱)。
因此,如果要访问全局对象,则应显式进行访问并通过window
引用。
首先,我认为您有些困惑。
在JavaScript中, 并非一切都是函数。 相反,可以使用几种不同的原语(从ES5开始):
推荐读物: Mozilla JavaScript数据结构文档
每种类型都有与之关联的特殊方法-除了null
和undefined
,它们都是具有非常特定用途的占位符-但是对于帮助您了解JavaScript的发生情况并不重要。 其中一个关键,这将有助于,是想法,JavaScript是什么叫做功能性的语言 。 在某种程度上,这意味着函数是一流的对象。 除其他外,这意味着可以将函数用作值。
另一件事是,返回的函数并不是返回的唯一对象 -发生的事情是函数编程是基于闭包或函数以及定义了返回函数的作用域中的所有相关变量的思想构建的(毕竟,我们可能在返回新函数的函数中引用变量!)
function foo() {
var bar = 3;
// Returns a function that when called, returns the Number 5.
return function () {
return 1 + 1 + bar;
}
}
至于缩小错误范围,在myObj.context
函数中,我注意到您已经编写了一个立即调用的表达式...
context: function () {
(function () {
this.afunc();
this.anotherfunc();
})();
},
那真的有必要吗? 实际上,该功能真的必要吗? 由于我们现在知道JavaScript将对象上的函数视为值,因此您始终可以只执行myObj.afunc
或myObj.anotherfunc
来处理特定于myObj
的代码。
如果您的意图是编写将在myObj
上调用afunc
和anotherfunc
方法的方法,则更好的代码集将是:
var myObj = {
afunc: function () {
// Magic!
},
anotherfunc: function () {
// More magic!
},
context: function () {
this.afunc();
this.anotherfunc();
}
};
如果您打算调用在myObj
声明周围的立即调用表达式中定义的afunc
/ anotherfunc
,只需从context
的调用中删除this
即可。
如果您打算在全局范围内调用afunc
/ anotherfunc
方法,那么您anotherfunc
走运了:您尚未在给定的代码段中定义此类函数。 但是,可以这样修复:
// Now, afunc exists in the global scope!
function afunc() {
// Magic
}
// Now, anotherfunc exists in the global scope!
function anotherfunc() {
// Magic
}
(function () {
var myObj = {
// Your object...
};
myObj.context();
})();
通常,这被认为是不好的编程形式,因为我们用不必要的函数,变量和对象使全局范围杂乱无章。 最好将函数声明保留在立即调用的对象内,除非您需要在其他地方调用它们。 如果您需要在其他地方调用这些函数,则可以考虑研究如何实现这些功能。
它不起作用,因为this
没有指代您以为所指的东西,而是保持在window
。 从那里开始,函数不在外部,因为它们被包装在(function(){ ... })();
。 现在到您的context
函数,我们不需要function()
包装器,因为全局范围内没有任何东西:
context:function(){
(function(){ // <-- Here
this.afunc();
this.anotherfunc();
})(); // <--
}
删除它(无论如何我们都不想要), this
将引用myobj
的实例
context:function(){
this.afunc();
this.anotherfunc();
}
如果我们要获取声明在myobj
范围之外的myobj
,则您不想使用this
。 因为这意味着函数属于myobj
的实例。 您只想说:
context:function(){
afunc();
anotherfunc();
}
因为myobj
不了解“全局”功能。
(function(){ var afunc=function (){ document.write('this is world'+'</br>'); } var anotherfunc=function (){ document.write('this is another world'); } var myobj={ afunc:function(){ document.write('afunc'); }, anotherfunc:function(){ document.write('anotherfunc'); }, context:function(){ afunc(); anotherfunc(); } }; myobj.context(); })();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.