本文介绍了从单独的文件扩展JavaScript类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很遗憾提出一个非常暗淡的问题,因为我对JavaScript中的OOP很新。

I'm sorry to ask quite a dim question as I'm very new to OOP in JavaScript.

我正在尝试使用,理想情况下我想将其存储在 utils.js 中,然后使用它(使用 Class.extend ),在我的项目使用的整个脚本文件范围内。这可能吗?我注意到如果我从 utils.js 文件中创建一个子类,我就可以创建一个 new 来自不同脚本的该类的实例,这使我认为它可能是可能的。它是否与包含在立即调用的函数表达式中的方法有关?

I'm trying to use John Resig's Simple JavaScript Inheritance method, and ideally I'd like to store this within say utils.js, and then use it (using Class.extend), throughout the range of script files that my project uses. Is this possible? I've noticed that if I create a subclass from within my utils.js file, I can then create a new instance of that class from a different script, so that makes me think it might be possible. Does it have something to do with the method being wrapped in an immediately-invoked function expression?

推荐答案

当然可以,只需在其余部分之前加载util.js文件。虽然如果我是你(你的项目中似乎开始有几个不同的文件),我会查看或其他一些AMD库在模块中分解您的项目。

Sure it's possible, just load the util.js file before the rest. ALthough if I were you (seems like you're starting to have several different files in your project) I'd check out http://requirejs.org/ or some other AMD library to break up your project in modules.

这篇关于从单独的文件扩展JavaScript类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 04:58