[英]How to make a javascript class with a constructor method to initiate new objects / instances?
I'm experimenting with javascript. 我正在尝试使用javascript。 I'm trying to make a "class" but it is not working like it should. 我正在尝试制作一个“课堂”,但它并没有像它应该的那样工作。 I have problems initiating a new instance of MyClass. 我在启动MyClass的新实例时遇到了问题。 See the js fiddle. 看到js小提琴。 Is it possible to add a constructor to MyClass just like in php? 可以像在php中一样向MyClass添加构造函数吗? I'm also not sure that this is the way "classes" are defined in javascript. 我也不确定这是在javascript中定义“类”的方式。 So any help is more then appreciated. 所以任何帮助都会受到更多的赞赏。
http://jsfiddle.net/kasperfish/JnvFS/ http://jsfiddle.net/kasperfish/JnvFS/
var MyClass={
test:'hello',
hallo: function(){
alert(this.test);
},
setTest: function(x){
this.test=x;
}
}
MyClass.hallo();
MyClass.setTest('world');
MyClass.hallo();
//not working because MyClass is/does not have a constructor
x= new MyClass;//*
x.hallo();
* firebug: TypeError: MyClass is not a constructor * firebug:TypeError:MyClass不是构造函数
In JavaScript, the constructor of a class is just a function, and the members are set either in there, or using the prototype
object. 在JavaScript中,类的构造函数只是一个函数,成员可以在那里设置,也可以使用prototype
对象。 For example: 例如:
function MyClass(someArgument) {
// construction, init members of this
this.someField = someArgument;
this.test = 'a test';
}
// in addition, all members of `prototype` are automatically set on every
// new instance of MyClass.
MyClass.prototype.hallo = function(){
alert(this.test);
};
and then 接着
var inst = new MyClass(42);
inst.hallo();
I suggest you read up on prototype-based OOP in JavaScript. 我建议你在JavaScript中阅读基于原型的OOP。 It is definitely different than the one you may be used to from PHP, and it generally does not feel very intuitive. 它绝对不同于您可能习惯使用的PHP,它通常不会非常直观。
You're gonna have to make your MyClass
Object a function like this: 你必须使你的MyClass
对象成为这样的函数:
var MyClass = function() {
this.test = 'Hello';
};
MyClass.prototype.hello = function() {
alert(this.test);
};
MyClass.prototype.setTest = function( x ) {
this.test = x;
};
var myInstance = new MyClass();
myInstance.hello();
myInstance.setTest('World');
myInstance.hello();
ok, firstly you should know that javascript does not have "classes" per say, javascript datastructures are indeed made of objects but as you can see from your code above, objects are essentially key-value pairs much unlike other languages like php, the implementation of OOP in javascript is very different. 好吧,首先你应该知道javascript没有“类”,javascript数据结构确实是由对象构成的,但正如你从上面的代码中看到的,对象本质上是键值对,与其他语言如php不同,实现在javascript中的OOP是非常不同的。
if you would like to use a constructor to create instances of your object, then try and create your "class" using a function, a construtor, maybe in this way: 如果你想使用构造函数来创建对象的实例,那么尝试使用函数,construtor创建你的“类”,也许这样:
function MyClass(){
this.test ='hello';
this.hallo = function(){
alert(this.test);
};
this.setTest = function(x){
this.test=x;
};
}
var myClassInstance = new MyClass();
myClassInstance.hallo();
myClassInstance.setTest('world');
myClassInstance.hallo();
i hope this helps 我希望这有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.