Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查变量是否未定义_Javascript_Jquery_Backbone.js_Haml_Undefined - Fatal编程技术网

Javascript 如何检查变量是否未定义

Javascript 如何检查变量是否未定义,javascript,jquery,backbone.js,haml,undefined,Javascript,Jquery,Backbone.js,Haml,Undefined,我正在使用HAML和jquery来编写我的应用程序。我有一个用于显示URL链接的“共享”模板。我希望它是灵活的,这样它将显示不同的URL取决于传递到它的变量 //Code to trigger view and populate template -> Note I only pass in the thread vara here share_view = new Onethingaday.Views.Threads.ShareView thread: new Onethingada

我正在使用HAML和jquery来编写我的应用程序。我有一个用于显示URL链接的“共享”模板。我希望它是灵活的,这样它将显示不同的URL取决于传递到它的变量

//Code to trigger view and populate template -> Note I only pass in the thread vara here
share_view = new Onethingaday.Views.Threads.ShareView
  thread: new Onethingaday.Models.Thread(@options.thread_muses.first().get('question'))

//snippet from share view to pass the variables to the template
render: ->
  $(@el).html @template
    nickname: @options.nickname
    thread_slug: @options.thread.get('slug')
    thread_text: @options.thread.get('text')
  @

//Snippet of share view's template. Added if-else statement to check if nickname is defined
.sidebar_text Link:
  <% if (typeof <%= nickname %> !== "undefined") { %>
  %input.detail_text{value: "<%= 'http://dailymus.es/' + nickname + '/threads/' + thread_slug %>"}
  <% } else { %>
  %input.detail_text{value: "<%= 'http://dailymus.es/' + '/threads/' + thread_slug %>"}
  <% } %>
//触发查看和填充模板的代码->注意我只在这里传递线程vara
share_view=new onethingday.Views.Threads.ShareView
thread:new-onethingday.Models.thread(@options.thread_muses.first().get('question'))
//从共享视图中提取代码片段以将变量传递给模板
渲染:->
$(@el.html@template
昵称:@options.昵称
thread\u slug:@options.thread.get('slug'))
thread\u text:@options.thread.get('text'))
@
//共享视图模板的片段。添加了if-else语句以检查是否定义了昵称
.sidebar_文本链接:
%input.detail_text{value:}
%input.detail_text{value:}
在上面的例子中,我没有定义“昵称”变量。我希望将触发else部分,并且显示的URL为“”

然而,我仍然得到以下信息

如您所见,它仍在触发“if”部分并将“undefined”显示为URL的一部分


在我的例子中,我如何正确地检查“未定义”变量?

当昵称已经包含在一个变量中时,您还需要将其包装起来吗

也许“昵称”设置为空?在这种情况下,它的类型将是“object”,而不是“undefined”。当它已经在一个控制台中时,您还需要将
昵称
包装在
中吗?@dbaseman,我做了一个控制台日志console.log(typeof@options.昵称),结果给了我“undefined”您确定条件运算符吗!==或者=@Karthi我两个都试过了但都没用