有谁知道为什么这会发生在下面的代码
if(myVarible !=undefined){ myVarible.doSomething() }
Run Code Online (Sandbox Code Playgroud)
myVariable是一个全局对象,仅在某些页面上使用我确信我过去已经完成了这个并且它总是有效.我也试过了
if(!!s){}
Run Code Online (Sandbox Code Playgroud)
我也确信我过去曾使用过.
终于搞定了 if(typeof s!=="undefined"){}
但我想知道为什么未定义的变量不等于undefined
它,为什么它在过去有用?
谢谢
我们如何区分var foo;和var foo=undefined;?
typeof foo将返回"undefined"并foo in window返回true;
(对不起,如果先前查询过,我没找到它)
我曾经检查过对象和方法是否存在并以这种方式调用它:
obj && obj.method && obj.method()
Run Code Online (Sandbox Code Playgroud)
但是,我怀疑在某些情况下这会给IE带来一些麻烦..
我需要使用它typeof undefined/function/object 吗?
typeof obj === 'object' && typeof obj.method === 'function' && obj.method()
Run Code Online (Sandbox Code Playgroud)
我想知道什么是最安全和最清晰的代码风格.
使用+=运算符为属性赋值给了我NaNJavaScript.
此代码按预期工作:
> var result = {};
undefined
> result['value'] = 10;
10
> result['value'] += 10;
20
Run Code Online (Sandbox Code Playgroud)
但是我们得到NaN:
> var test = {};
undefined
> test['value'] += 10;
NaN
Run Code Online (Sandbox Code Playgroud)
为什么JavaScript的行为如此?如何在不初始化的情况下使其工作result['value'] = 0?
我正在学习Javascript,我遇到了一些"古怪"的东西.
为什么不是类型.length或类型的属性.name?它似乎应该属于那一类.相反,它被认为是一个类似于等号的运算符=
也许有一个明显的解释或我不理解的东西(很容易).
我有以下javascript代码:
var currentIds = localStorage.getItem('currentPairsIds');
if ((typeof currentIds === "undefined") ||
(currentIds == null))
$.myNameSpace.currentIDs = new Array(3);
else
$.myNameSpace.currentIDs = currentIds.Split(',');
Run Code Online (Sandbox Code Playgroud)
我正在使用Firebug进行调试,虽然currentIds没有任何值,但它始终执行else语句.
更新:
我从HTML5存储中获取此值.
我究竟做错了什么?
我使用jwt-simple lib得到以下错误:
TypeError: Cannot read property 'split' of undefined
at module.exports (C:\my_application\services\mylist.js:5:40)
at Layer.handle [as handle_request] (C:\my_application\node_modules\express\lib\router\layer.js:95:5)
at next (C:\my_application\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\my_application\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\my_application\node_modules\express\lib\router\layer.js:95:5)
at C:\my_application\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\my_application\node_modules\express\lib\router\index.js:330:12)
at next (C:\my_application\node_modules\express\lib\router\index.js:271:10)
at C:\my_application\api.js:39:3
at Layer.handle [as handle_request] (C:\my_application\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\my_application\node_modules\express\lib\router\index.js:312:13)
at C:\my_application\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\my_application\node_modules\express\lib\router\index.js:330:12)
at next (C:\my_application\node_modules\express\lib\router\index.js:271:10)
at logger (C:\my_application\node_modules\morgan\index.js:144:5)
at Layer.handle [as handle_request] (C:\my_application\node_modules\express\lib\router\layer.js:95:5)
Run Code Online (Sandbox Code Playgroud)
这是mylist.js文件:
var jwt = require('jwt-simple');
module.exports = function (req, res) {
var token = req.headers.authorization.split(' ')[1];
var payload …Run Code Online (Sandbox Code Playgroud) 我如何检查 js 链 var 是否存在?任何简单的检查方法,或使用 jquery
请参阅下面的代码:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
// how can i check a var exists ?
// the code bellow will return undefined
// a is undefined
// a.b is undefined --- when a exists, then I need to check a.b
// a.b.c is undefined ...
// a.b.c.d is undefined
// sometimes I need to check if some property of an object exists or is true, and I don't even know the object exists or …Run Code Online (Sandbox Code Playgroud) 我正在使用谷歌地图api v3并在使用时遇到此错误
map.fitBounds(边界); 功能
以下是代码:
var arr = [{lat: -25.363, lng: 131.044}, {lat: 12.97, lng: 77.59}];
for (i = 0; i < arr.length; i++) {
bounds.extend(new google.maps.LatLng(arr[i]));
}
map.fitBounds(bounds); //If I comment this line in m code, the error is gone but map does not load.
Run Code Online (Sandbox Code Playgroud)
问题是什么?另外我该如何解决?
可能重复:
在JavaScript中检测未定义的对象属性
会这样吗?
if(variable == undefined) {
Run Code Online (Sandbox Code Playgroud)
还是会这样?
if(variable == "undefined") {
Run Code Online (Sandbox Code Playgroud) 有一些例子:
a = ''; //string
b = 0; //number 0
b1 = 0xf; //number 15
c = (function(){}) //function function (){}
d = []; //object
e = {}; //object [object Object]
f = void(0); //undefined undefined
Run Code Online (Sandbox Code Playgroud)
但是当我尝试传递未定义的变量trougth函数时:
typeof qwerty; //undefined
function at(a){return (typeof a)+' '+a;}
at(qwerty); // ????
Run Code Online (Sandbox Code Playgroud)
..i`v收到错误"Uncaught ReferenceError:qwerty未定义".我怎样才能(是否存在最短路径)创建函数isDefined(a,b)或减少该表达式的其他技巧?:
c=(typeof a!='undefined'&&a||b)
Run Code Online (Sandbox Code Playgroud)
澄清:如果a被定义--c等于a,overwise -b,就像php中的"c = @ a?:b"
编辑:
function ud(_a){return typeof window[_a]==='undefined'}
a=undefined; b=3;
alert((ud('a')?4:a)+(ud('b')?5:b));?
Run Code Online (Sandbox Code Playgroud)