gpt4 book ai didi

javascript - Meteor JS 共享方法和对象

转载 作者:行者123 更新时间:2023-11-30 16:37:46 25 4
gpt4 key购买 nike

我的问题

在使用 Meteor JS 时,我遇到了一些我不太确定如何修复的问题,因为所有内容似乎都按校正顺序加载。它仍然没有以正确的方式行事,它说函数未定义。

我查看了其他问题,其中大部分涉及 meteor 方法部分。我有点困惑为什么我需要将它包装在一个 Method 对象中以便所有它们都被使用。

我的问题是如何在不使用 meteor 方法调用并将它们作为子对象放入的情况下将多个函数和“人造”类用作共享 javascript 文件。

我正在尝试用作共享 js 的示例类。

function CustomerProjects(){
// declare variables
this.name = null;
this.scope = null;
this.time = null;
this.completed = false; // set every project to default false
this.description = null;

// Get the time array
this.billableHours = function(){
if(this.completed){
var totalTime = 0; // int
var time = this.time;
// calculate time
for(var i = 0; i < time.length; i++){
totalTime = totalTime + time[i];
}
return totalTime;
} else {
return "Not Completed";
}
};
}

我的文件结构

fasic file structure

最佳答案

您的定义是文件范围的,将其更改为全局范围:

function CustomerProjects(){  // file scoped
..

CustomerProjects = function (){ // global scoped
..

关于javascript - Meteor JS 共享方法和对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32460651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com