使用browserify,未定义的ReferenceError:函数未定义 [英] Using browserify, Uncaught ReferenceError: function is not defined
问题描述
我正在尝试 http://browserify.org/ 中的示例,并尝试进行如下函数调用:
我的html是:
<!DOCTYPE html>
< html>
< head>
< title> Test Browserify< / title>
< script src = bundle.js>< / script>
< / head>
< body>
< button onclick = hello()> test< / button>
< / body>
< / html>
而我的JavaScript是:
var unique = require('uniq');
var data = [1、2、2、3、4、5、5、5、6];
console.log(unique(data));
函数hello(){
alert( here);
}
我确实做了browserify main.js -o bundle.js,所以我可以使用
但是当我单击按钮时,出现错误:
未捕获的ReferenceError:您好未定义
任何建议将不胜感激!
Browserify的主要目的是使JavaScript模块处于私有范围内,因此无法查看您要执行的操作。
尝试使用
global.hello = function(){alert( hello);}
请参见为browserify定义全局变量。 / p>
通常,这是一种不好的做法,您应该将公共属性导出到模块之外,然后通过所需的模块引用对其进行引用。
I am trying example in http://browserify.org/ and try to make a function call as follows:
My html is:
<!DOCTYPE html>
<html>
<head>
<title>Test Browserify</title>
<script src="bundle.js"></script>
</head>
<body>
<button onclick="hello()">test</button>
</body>
</html>
and my javascript is:
var unique = require('uniq');
var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];
console.log(unique(data));
function hello(){
alert("here");
}
I did browserify main.js -o bundle.js, so I can use require successfully.
But when I click the button, I have the error:
"Uncaught ReferenceError: hello is not defined"
Any suggestion will be appreciated!
Browserifies primary purpose is to make JavaScript modules privately scoped so it has no way to see what you are trying to do.
Try using
global.hello = function() { alert("hello");}
See defining global variable for browserify.
In general, this is bad practice and you should instead export public properties out of your module and reference them via the required module reference.
这篇关于使用browserify,未定义的ReferenceError:函数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!