您现在的位置是:首页 > 电脑技术查询 > web开发

js文件调用js文件,调用失败,被调用的方法为undefined。该怎么解决

编辑:chaxungu时间:2022-10-02 23:14:40分类:web开发

js文件调用js文件,调用失败,被调用的方法为undefined。
两个js文件:
car.js
JScript codevar car = function(brand, color) { this.start() { }}

people.js
JScript codevar people = function() { this.startCar() { var car = new car("大众", "黑色"); car.start(); }}我把这两个js按照先后顺序在页面head中引用,但是在new car的时候,报错说car undefined。
然后我又在people的开头写入:document.write("<script type='text/javascript' src='car.js'></script>"),并且在页面中指引用people.js,但还是一样的错误。
再后来,我干脆把car.js里的代码复制到people中,但还是同样的错误。
特上来发帖求救,在线等待答案。
谢谢各位。

------解决方案--------------------
var people = function() {
this.startCar() {
var c = new car("大众", "黑色");
c.start();
}
}
这样试试
------解决方案--------------------
JScript codevar car = function(brand, color) { this.start = function() { }}var people = function() { this.startCar = function() { var car = new car("大众", "黑色"); car.start(); }}
------解决方案--------------------
JScript codevar car = function(brand, color) {}car.prototype = { start:function(){}}var people = function() { this.startCar = function() { var car = new car("大众", "黑色"); car.start(); }}
------解决方案--------------------
function car(brand, color){
this.start() {
};
};
------解决方案--------------------
你对对象的属性怎么构造还没有弄清楚
JScript codevar car = function(brand, color) { this.start=function(){ } }; var people = function() { this.startCar=function(){ var car = new car("大众", "黑色"); car.start(); } } var a=new car();