[英]JavaScript Class Definition in Multiple Files
I'm learning JavaScript. 我正在学习JavaScript。 I come from a C# background. 我来自C#背景。 In C#, there is the concept of partial classes. 在C#中,存在子类的概念。 In other words, a way to split the definition of a class across files. 换句话说,一种跨文件分割类定义的方法。 Is there a way to do this in JavaScript? 有没有办法用JavaScript做到这一点?
Currently, I have a directory structure like this: 目前,我有这样的目录结构:
/
MyClass.js
child-folder
MyClassAdditions.js
MyClass.js MyClass.js
function MyClass() {
}
Is there a way to add additional functions to MyClass from MyClassAdditions.js
? 有没有办法从MyClassAdditions.js
向MyClass添加其他功能? If so, how? 如果是这样,怎么办?
Update 2018-05-21 - JavaScript now has formal classes, and the syntax looks like this: 更新2018年5月21日-JavaScript现在具有正式类,其语法如下所示:
class MyClass {
constructor(param1) {
this.param1 = param1;
}
// This is the same as MyClass.prototype.printParams
printPrams() {
console.log("The value of param1 is", this.param1);
}
}
Original answer from 2015: 2015年的原始答案:
While JS currently doesn't have formal classes, you can create constructor objects which can get instantiated. 尽管JS当前没有正式的类,但是您可以创建可以实例化的构造函数对象。 You can even extend these objects or add onto them later. 您甚至可以扩展这些对象或稍后添加它们。 Here's a simple example: 这是一个简单的例子:
// create your constructor
var MyClass = function(param1) {
// create instance properties here
this.param1 = param1;
};
// all instances of your class will point to the constructors prototype
MyClass.prototype.printParams = function () {
console.log("The value of param1 is", this.param1);
};
// lets create two instances so we can see how the prototype thing works
var foo = new MyClass("foo");
var bar = new MyClass("bar");
foo.printParams(); // => The value of param1 is foo
bar.printParams(); // => The value of param1 is bar
foo.param1 = "oof";
foo.printParams(); // => The value of param1 is oof
// Now, lets change how printParams works.
// Remember, we still have instances of foo and bar already created.
// Since they both point to their constructors prototype, you
// can change things later... at any time.
MyClass.prototype.printParams = function () {
console.log("PARAM1 SAID WHAT??", this.param1);
};
MyClass.prototype.sayNothing = function () {
console.log("nothing");
};
// All instances get these new methods, yay prototypal inheritance
foo.printParams(); // => PARAM1 SAID WHAT?? oof
bar.printParams(); // => PARAM1 SAID WHAT?? bar
foo.sayNothing(); // => nothing
bar.sayNothing(); // => nothing
// Lets say we want foo to have it's own sayNothing method,
// you can define one on the instance itself - not really cool, but doable
foo.sayNothing = function () {
console.log("nothing at all");
// If you want to be cool, you can call the shared prototype method too
this.constructor.prototype.sayNothing.call(this);
};
// bar is still going to use the method defined on the prototype
// while foo will have its own implementation of sayNothing
foo.sayNothing(); // => nothing at all
// => nothing
bar.sayNothing(); // => nothing
Since I have had similar problem I recommend this way which I think you can find nowhere, I found every part in a separated document: 由于我遇到过类似的问题,因此我建议您以这种方式找不到任何地方,因此我在单独的文档中找到了每个部分:
MyClass = {}
MyClass.js
file which is supposed to deal with laptop info, you can define some functions and properties like this: 然后,在应该用于处理笔记本电脑信息的MyClass.js
文件中,您可以定义一些函数和属性,如下所示: MyClass.laptop = { brand : "Dell", model: "1525", color: "silver", getBrandAndModel: function(){ return MyClass.laptop.brand + ", " + MyClass.laptop.model; }, getBrandAndColor: function(){ return MyClass.laptop.brand + ", " + MyClass.laptop.color; }, colorizeIt: function(colorName){ return MyClass.laptop.brand + ", " + colorName; } }
MyClassAdditions.js
which you are going to deal with cellphone info you go like this: 现在在MyClassAdditions.js
,您将要处理手机信息,如下所示: MyClass.cellphone = { brand : "Samsung", color: "Black", width: 8, height: 15, getDimention: function(){ return MyClass.cellphone.width + " * " + MyClass.cellphone.height; }, }
in this way, you have added two parts in two separated js files into an object named MyClass . 通过这种方式,您已将两个独立的js文件中的两个部分添加到名为MyClass的对象中。
Now test this examples to make sure it really works: 现在测试以下示例,以确保它确实有效:
MyClass.laptop.brand //"Dell"
MyClass.laptop.getBrandAndColor() //"Dell, silver"
MyClass.cellphone.color //"Black"
MyClass.cellphone.getDimention() //"8 * 15"
It's like you have created following object in the first place: 就像您首先创建了以下对象:
MyClass = {
laptop : {
brand : "Dell",
model: "1525",
color: "silver",
getBrandAndModel: function(){
return MyClass.laptop.brand + ", " + MyClass.laptop.model;
},
getBrandAndColor: function(){
return MyClass.laptop.brand + ", " + MyClass.laptop.color;
},
colorizeIt: function(colorName){
return MyClass.laptop.brand + ", " + colorName;
}
},
cellphone :{
brand : "Samsung",
color: "Black",
width: 8,
height: 15,
getDimention: function(){
return MyClass.cellphone.width + " * " + MyClass.cellphone.height;
},
}
}
By the way you can delete a part of object by the keyword delete
like this: 顺便说一句,您可以通过关键字delete
对象的一部分,如下所示:
delete MyClass.cellphone
now MyClass.cellphone
returns undefined
while MyClass.laptop
returns object. 现在MyClass.cellphone
返回undefined
而MyClass.laptop
返回对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.