我们可以在另一个JS文件中调用写在一个JS文件中的函数吗?任何人都可以帮我如何从另一个JS文件调用该函数?
根据这个答案我的函数调用应该工作,但它不是:在另一个js文件中调用javascript函数
我的代码(简化):
file1.js:
$(document).ready(function(){
function loadFYfResults(sizes){
alert('hello');
}
});
Run Code Online (Sandbox Code Playgroud)
file2.js
$(document).ready(function(){
$('.size-button').click(loadFYfResults(sizes));
});
Run Code Online (Sandbox Code Playgroud)
head.liquid
在头部:
{{'file1.js'| asset_url | script_tag}}
{{'file2.js'| asset_url | script_tag}}
当调用该函数时,控制台会抛出此错误:
"Uncaught ReferenceError:loadFyfResults未定义"
我的网站是使用Shopify的液体主题平台构建的,但我不确定它是否与它有任何关系.谁能发现我做错了什么?
<html>
<head>
<script src="./first.js"></script>
<script src="./second.js"></script>
</head>
</html>
Run Code Online (Sandbox Code Playgroud)
在first.js文件中,我想从second.js调用函数:
secondFun(); // calling a function from second.js file
Run Code Online (Sandbox Code Playgroud)
这是 second.js 文件:
function secondFun() {
console.log('second function called!!')
}
Run Code Online (Sandbox Code Playgroud) 此代码无法在最后一行运行.我不知道为什么.
var Vehicle = function() {
var age = 21; //private variable
this.setAge = function(age2) {age = age2;};
this.getAge = function() {return age;};
};
var Plane = function() {};
Plane.prototype = Object.create(Vehicle.prototype);
var plane = new Plane();
console.log( plane instanceof Vehicle );
//console.log( plane.getAge() ); //TypeError: plane.getAge is not a functionRun Code Online (Sandbox Code Playgroud)