简体   繁体   English

如何使用构造函数方法创建一个javascript类来启动新对象/实例?

[英]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. 看到j​​s小提琴。 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.

相关问题 javascript:制作新的安全类构造函数 - javascript : make a new safe class constructor 如何让类构造函数在Node.js中创建新实例时发出事件 - How to have a class constructor emit a event on creating new instances in nodejs 从构造函数初始化单个实例,并将其用作另一个类中的静态方法 - Initiate single instance from a constructor and use it as static method in another class 什么时候在JavaScript的构造函数和类中创建新对象? - When are new objects created in JavaScript's constructor function vs in class? JavaScript使类的方法静态且可用于其实例 - JavaScript make a class's method static and available to its instances 如何在将 class 添加到 Javascript 中的新 object 时删除其所有实例 - How to remove all instances of a class when adding it to a new object in Javascript 如何使用类语法复制Javascript构造函数和原型对象 - How to replicate Javascript Constructor & Prototype objects with Class Syntax Javascript类构造函数调用方法 - Javascript Class Constructor Call Method 如何为 javascript 中的 class 的所有实例制作吸气剂代理? - How to make a getter proxy for all instances of a class in javascript? javascript:如何为某个类的所有实例覆盖方法? - javascript: How to override a method for all the instances of some class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM