Javascript angular.isdefined 有什么好处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27818331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:42:55  来源:igfitidea点击:

What is the benefit of angular.isdefined?

javascriptangularjs

提问by Ben Aston

What is the benefit of angular.isdefinedover and above foo === undefined?

angular.isdefinedover and above 有foo === undefined什么好处?

I can't immediately think of a benefit.

我想不出有什么好处。

采纳答案by Bharath Kumar Bachina

Accessing a truly undefined variable in any way in Javascript, except typeof throws an error. You can only use Angular.isDefinedwith properties. E.g, this would work fine:

在 Javascript 中以任何方式访问真正未定义的变量,除了 typeof 会引发错误。您只能Angular.isDefined与属性一起使用。例如,这可以正常工作:

angular.isDefined(window.obj);

Because obj is an undefined propery of window.

因为 obj 是 window 的未定义属性。

Examples of the expected behavior:

预期行为的示例:

var foo;
var bar = 42;

typeof foo !== 'undefined'; // false
typeof bar !== 'undefined'; // true
typeof baz !== 'undefined'; // false

angular.isDefined(foo); // false
angular.isDefined(bar); // true
angular.isDefined(baz); // ReferenceError

回答by Matt Way

Here is the source:

这是来源:

function isDefined(value) {return typeof value !== 'undefined';}

Obviously the first reason is a lower verbosity, but it also future proofs angular, especially if the function is used internally.

显然第一个原因是较低的冗长性,但它也可以在未来证明 angular,尤其是在内部使用该函数时。

回答by Carlos Verdes

Like Kamrul said angular does:

就像 Kamrul 所说的 angular 那样:

function isDefined(value) { return typeof value !== 'undefined'; }

Which means "the type of this var is undefined"... in you example you compare the content of the variable is equals to undefined and angular is checking the type of the variable.

这意味着“此变量的类型未定义”...在您的示例中,您比较变量的内容是否等于未定义,而 angular 正在检查变量的类型。

In js the types are dynamic so until you don't assign a value the variable has no type... so isDefined will tell you both, if a variable declaration exist and if this variable has any content.

在 js 中,类型是动态的,所以在你不赋值之前,变量没有类型......所以 isDefined 会告诉你两者,如果变量声明存在以及这个变量是否有任何内容。

But, be careful because the variable could be null, in which case the type of the variable would be object.

但是,要小心,因为变量可能为空,在这种情况下变量的类型将是对象。

You could try this code:

你可以试试这个代码:

var a;
var b = 'asd';
var c = null;

console.log('a: ' + typeof a);
console.log('b: ' + typeof b);
console.log('c: ' + typeof c);
console.log('d: ' + typeof d);

And you will see the next in console:

您将在控制台中看到下一个:

a: undefined
b: string 
c: object 
d: undefined

So,

所以,

a) the var exist but has no value so is undefined

a) var 存在但没有值所以未定义

b) the var exist and has value.. this value is a string so this is its type

b) var 存在并且有值。这个值是一个字符串,所以这是它的类型

c) the var exist but is null, the type can't be interfered so its type is object

c) var 存在但为空,类型不能被干扰,所以它的类型是对象

d) the var has not been declared so... it's undefined

d) var 还没有被声明所以......它是未定义的

The main point is the diference between "a" and "d"... so try the next:

重点是“a”和“d”之间的区别......所以尝试下一个:

console.log('a is undefined? ' + a === undefined);
console.log('d is undefined? ' + d === undefined);

You will see the next in console:

您将在控制台中看到下一个:

false
Uncaught ReferenceError: d is not defined

Which... is a big problem because:

这是一个大问题,因为:

a) tells you that is not undefined when that's not true

a) 告诉你这不是 undefined 当那不是真的

d) raise an exception so... you code will fail

d) 引发异常,因此...您的代码将失败

Conclusion

结论

Use is defined when you want to check if a variable exists and has been initialized with a value, but be careful with null values because null is an object (so is a defined var).

当你想检查一个变量是否存在并且是否已经用一个值初始化时定义了使用,但要小心空值,因为空是一个对象(定义的变量也是如此)。

If you want to validate that a variable exists and has any valid value (so is not null) you can simple do something like:

如果要验证变量是否存在并且具有任何有效值(因此不为空),您可以简单地执行以下操作:

if (myvar) {
  console.log('myvar is defined and is not null');
} else {
    console.log('myvar is undefined or null');
}

Another good trick is to init to some value if the var is not defined with ||

另一个很好的技巧是,如果 var 没有用 || 定义,则初始化为某个值

myvar = myvar || 'some init value';

The above code takes the value of myvar if is defined and not null and if not init it with some value.

上面的代码采用 myvar 的值,如果已定义且不为空,如果未使用某个值初始化它。

As @TonyBrasunas put on his comment if myvar is evaluated to false, 'some init value' will be assigned. Take this into consideration before using this trick.

正如@TonyBrasunas 在评论中所说,如果 myvar 被评估为 false,将分配“一些初始值”。在使用这个技巧之前考虑到这一点。

This is good in functions, for example:

这在函数中很好,例如:

function split(input, charToSplit) {
  charToSplit = charToSplit || ' ';
  return input.split(charToSplit);
}

Then by default you can split with whitespaces: var input = 'asd asd'; var splited = split(input); // --> splited = ['asd', 'asd']

然后默认情况下你可以用空格分割: var input = 'asd asd'; var splited = split(输入); // --> splited = ['asd', 'asd']

Or... with another char:

或者...使用另一个字符:

var input = 'asd|asd';
var splited = split(input, '|');
// --> splited= ['asd', 'asd']

回答by Jason Swett

I can only guess but I think my guess is a pretty good one.

我只能猜测,但我认为我的猜测是一个很好的猜测。

These two expressions are functionally equivalent:

这两个表达式在功能上是等价的:

typeof foo !== 'undefined'

angular.isDefined(foo)

Benefits of the latter include:

后者的好处包括:

1) It's arguably less of a mental strain to ask whether something is definedthan to ask if something is not undefined.

1) 可以说,询问某事是否已定义比询问某事是否未定义更容易造成精神压力。

2) angular.isDefined(foo)is arguably a lot less "noisy" than typeof foo !== 'undefined', and therefore it's quicker to grasp what's happening.

2)angular.isDefined(foo)可以说比 更“嘈杂” typeof foo !== 'undefined',因此可以更快地掌握正在发生的事情。

Note: These aren't myarguments for the superiority of angular.isDefined. What I'm trying to convey is my guess as to why the Angular team wanted to create angular.isDefinedand why they thought it was better than the plain JavaScript alternative.

注意:这些不是angular.isDefined. 我想传达的是我对 Angular 团队为什么想要创建angular.isDefined以及为什么他们认为它比普通 JavaScript 替代方案更好的猜测。