简体   繁体   English

JavaScript typeof Typeahead返回未定义

[英]Javascript typeof Typeahead returns undefined

I'm loading the bootstrap Typeahead plugin prior to calling Typeahead in my script, but typeof Typeahead return undefined even when it's absolutely loaded. 我在脚本中调用Typeahead之前先加载Bootstrap Typeahead插件,但是即使绝对加载,typeof Typeahead仍返回未定义。

Javascript file line 2650: JavaScript文件第2650行:

!function($){"use strict";var Typeahead=function(element,options) // ... rest of plugin

Javascript file line 2765: JavaScript文件第2765行:

alert(typeof Typeahead);

alerts undefined 警报未定义

why would this be the case? 为什么会这样呢?

You don't really show us enough of the code. 您没有真正向我们展示足够的代码。 But, if the: 但是,如果:

alert(typeof Typeahead);

is outside of the function block where var Typeahead = function() {...} appears, then you are outside the scope where that variable (and thus function) is defined so the variable will be undefined . 在出现var Typeahead = function() {...}的功能块之外,那么您在定义该变量(因此定义函数)的范围之外,因此该变量将是undefined Variables are only visible within the scope in which they are defined. 变量仅在定义它们的范围内可见。

If you want var Typeahead to be available outside that function block, then it must be declared at a higher scope. 如果希望var Typeahead在该功能块之外可用,则必须在更高范围内声明它。

As an example: 举个例子:

function foo() {
   // define local variable only visible within function foo
   var greeting = "Hi";
}

console.log(typeof greeting);   // will show "undefined"

// define higher scope variable visible in foo and outside of foo
var greeting = "Hi";

function foo() {
    console.log(greeting);      // will show "Hi"
}

console.log(greeting);          // will show "Hi"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM