admin管理员组文章数量:1334808
This question is specific to angular.isDefined() being named isDefined. We don't expect it to find a way to return false if the variable passed to it is not defined rather than throwing an uncaught error. I am looking for an angular equivalent of typeof operator to detect the not Defined variables.In the below example x
is not Defined and typeof x
doesn't throw any Uncaught ReferenceError: x is not defined
error.where in if i had used angular.isDefined
i would have received a not defined error
.
if(typeof x != 'undefined')
alert("defined");
else
alert("undefined");
angular.isDefined(x) throws Uncaught ReferenceError: x is not defined
if x is not Defined .
if(angular.isDefined(x)) // throws error because x is not defined
alert("defined");
else
alert("undefined");
var x;
if(angular.isDefined(x)) // does not throw error as x is defined though not **initialiased**
alert("defined");
else
alert("undefined");
Is there an angular alternative to typeof x !='undefined'
.Let me explain how am i ending up with this scenario.I have createContext()
function in a js file which i am not allowed to modify.and this function defines context="something useful"
and i have two controllers monController
and contextController
.monController
is executed first and registers for an custom eventhandler which needs access to context
.then monController
executes which calls createContext()
.sometimes depending on the execution time even before the createContext()
is called.the eventHadler is triggered and would try to access the variable which is not yet defined. I can not call createContext()
anywhere else other than in contextController
.
This question is specific to angular.isDefined() being named isDefined. We don't expect it to find a way to return false if the variable passed to it is not defined rather than throwing an uncaught error. I am looking for an angular equivalent of typeof operator to detect the not Defined variables.In the below example x
is not Defined and typeof x
doesn't throw any Uncaught ReferenceError: x is not defined
error.where in if i had used angular.isDefined
i would have received a not defined error
.
if(typeof x != 'undefined')
alert("defined");
else
alert("undefined");
angular.isDefined(x) throws Uncaught ReferenceError: x is not defined
if x is not Defined .
if(angular.isDefined(x)) // throws error because x is not defined
alert("defined");
else
alert("undefined");
var x;
if(angular.isDefined(x)) // does not throw error as x is defined though not **initialiased**
alert("defined");
else
alert("undefined");
Is there an angular alternative to typeof x !='undefined'
.Let me explain how am i ending up with this scenario.I have createContext()
function in a js file which i am not allowed to modify.and this function defines context="something useful"
and i have two controllers monController
and contextController
.monController
is executed first and registers for an custom eventhandler which needs access to context
.then monController
executes which calls createContext()
.sometimes depending on the execution time even before the createContext()
is called.the eventHadler is triggered and would try to access the variable which is not yet defined. I can not call createContext()
anywhere else other than in contextController
.
- No, there is not. But I can't see any case where the difference matters, since your variables will probably always be declared. – Blackhole Commented Mar 18, 2015 at 9:54
-
yes i have a scenario wherein the code inside a event subscribing block
$on
is accessing a variable defined in another function which may be executing at that moment and has not finished executing. – Divya MV Commented Mar 18, 2015 at 10:05 - @Blackhole there is a little difference i checked the Doc of angular. – squiroid Commented Mar 18, 2015 at 10:16
-
1
@DivyaMV that's impossible, JavaScript is single threaded hence either the
$on
will be executed before or after the code it needs access to. – thomaux Commented Mar 18, 2015 at 10:33 -
1
@DivyaMV A variable doesn't go in and out of scope depending on whether a function has executed or not (unless you're adding properties to the
window
object or something like that). The scenario you are describing makes zero sense. – JLRishe Commented Mar 19, 2015 at 19:48
3 Answers
Reset to default 3As you have now clarified in the ments in @squiroid's answer, your canvasDesign
function looks like this:
canvasDesign = function() {
var canvas1 = document.getElementById("canvas1");
if(canvas1 != null) context1 = canvas1.getContext('2d');
}
That function is creating an implicit global variable (because it's not using var
on context1
).
That is a bad practice, which is forbidden in ES5 strict mode, and the correct thing to do is to fix the code to stop using antipractices. If that is not an option, your best bet is to continue to use typeof context1 === 'undefined'
. As this question clarifies, it's impossible to create a general function to check for the existence of a variable.
Two other options:
- Use
angular.isDefined(window.context1)
. This will allow you to check for a global variable without throwing an error, but this approach will break if at some point the variable is made non-global. - Put
var context1;
at the top of your file, outside any functions. This will ensure that the variable is at least declared, and allow you to callangular.isDefined(context1)
safely.
angular.isDefined(obj) is not a plete substitute for typeof.
isDefined according to doc :
function isDefined(value) {return typeof value !== 'undefined';}
Which clearly represent it is just using typeof on the backend but with reference strict type checking(!== not !=).
So in your case typeof is working while angular.isDefined() not.
PS:- Depend on your requirement you can use typeof or angular.isDefined().
Your question "Is there an angular alternative to typeof x !='undefined'
" doesn't make sense.
The way JavaScript (irrespective of Angular) works is that it would be an error to do the following:
function foo(x){ return true; }
foo(a); // this will cause "Uncaught ReferenceError: a is not defined"
Angular's isDefined
is a function, like any other, and when invoked with an undeclared variable, causes an error.
本文标签: javascriptangularisDefined vs typeofStack Overflow
版权声明:本文标题:javascript - angular.isDefined vs typeof - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742345082a2457360.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论