[英]explicit typeof == “undefined” check vs just checking for its existence?
assuming x is an object... Is there any benefit of doing: 假设x是一个对象......有什么好处:
if (typeof x.foo != "undefined")
vs. doing 与做
if (x.foo)
? ?
This question came up as I was reading this blog post: http://www.nczonline.net/blog/2010/03/09/custom-events-in-javascript/ 我在阅读这篇博文时提出了这个问题: http : //www.nczonline.net/blog/2010/03/09/custom-events-in-javascript/
In his example, he does: 在他的例子中,他做了:
function EventTarget(){
this._listeners = {};
}
EventTarget.prototype = {
constructor: EventTarget,
addListener: function(type, listener){
if (typeof this._listeners[type] == "undefined"){
this._listeners[type] = [];
}
this._listeners[type].push(listener);
In this case this._listeners[type] will never be anything except an array-- so is it not true that it would be cleaner in this case to just do 在这种情况下,this._listeners [type]将永远不会是除数组之外的任何东西 - 所以在这种情况下它只是做得更干净
addListener: function(type, listener){
if (!this._listeners[type]){
this._listeners[type] = [];
}
this._listeners[type].push(listener);
? ?
Also, as a side question, I don't get why he's doing: 另外,作为一个附带问题,我不知道他为什么这样做:
EventTarget.prototype = {
constructor: EventTarget
Isn't the constructor by default already set to EventTarget ('this') when you call new EventTarget() ? 调用new EventTarget()时,默认情况下构造函数是否已设置为EventTarget('this')?
Watch out for truthy values . 注意真正的价值观 。
if (x.foo)
will not run if x.foo is if (x.foo)
x.foo是, if (x.foo)
将不会运行
Where as if (typeof x.foo !== "undefined") {
only checks for whether the value is undefined
if (typeof x.foo !== "undefined") {
仅检查值是否undefined
Alternative checks would be 替代检查将是
if (x.foo !== undefined) {
and if (x.foo !== void 0) {
if (x.foo !== undefined) {
和if (x.foo !== void 0) {
Do be wary that undefined
can be overwritten as a local variable 要小心,
undefined
可以被覆盖为局部变量
undefined = true
is a valid statement and will break all your code. undefined = true
是一个有效的语句,将破坏您的所有代码。 Of course you will never see this code in production so you don't really have to shield against it, it's just something to be wary of. 当然,你永远不会在生产中看到这个代码,所以你真的不必屏蔽它,它只是需要警惕的东西。
I personally tend to use 我个人倾向于使用
if (x.foo != null) {
...
}
a lot which checks for both null
and undefined
. 检查
null
和undefined
的很多东西。
[[Edit]] [[编辑]]
In your specific example it's either an Array
or undefined
so !foo
is safe. 在您的具体示例中,它是一个
Array
或undefined
所以!foo
是安全的。 personally I prefer to check specifically for undefined
so that users know I only expect it to run when it's undefined rather then when it's null
or false
or ""
. 我个人更喜欢专门检查
undefined
以便用户知道我只希望它在未定义时运行,而不是在它为null
或false
或""
。 This makes the code more explicit / self documenting. 这使代码更加明确/自我记录。
As to 至于
EventTarget.prototype = {
constructor: EventTarget
If you overwrite EventTarget.prototype
with a new object then the EventTarget.prototype.constructor
property is lost and needs to be set again. 如果使用新对象覆盖
EventTarget.prototype
,则EventTarget.prototype.constructor
属性将丢失,需要再次设置。
You do not need to set .constructor
again if you just extend the prototype by calling EventTarget.prototype.method = ...
. 如果只是通过调用
EventTarget.prototype.method = ...
扩展原型,则不需要再次设置.constructor
。
The first explicitly checks that x.foo
is undefined
, whereas if (x.foo)
is checking to see if x.foo
is truthy. 第一个显式检查
x.foo
是否undefined
,而if (x.foo)
正在检查x.foo
是否真实。
http://11heavens.com/falsy-and-truthy-in-javascript http://11heavens.com/falsy-and-truthy-in-javascript
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.