Javascript - 如何定义构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5030739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:20:12  来源:igfitidea点击:

Javascript - How to define a Constructor

javascript

提问by user256034

I have an object written like this:

我有一个这样写的对象:

Object1.prototype = {
    isInit: false,
    Get : function (){}
} 

Now I'd like to add a constructor which takes one parameter. How can I do it?

现在我想添加一个带有一个参数的构造函数。我该怎么做?

回答by Andrew Orsich

Class declaration

类声明

var User = function(name, age) { // constructor
}

User.prototype = {}

Instance variables (members)

实例变量(成员)

var User = function(name, age) {
    this.name = name;
    this.age = age;
}

User.prototype = {}

Static variables

静态变量

var User = function(name, age) {
    this.name = name;
    this.age = age;
}

User.prototype = {
    staticVar: 15,
    anotherStaticVar: 'text'
}

Here I defined two static variables. Each User instance has access to these two variables. Note, that we can initialize it with value;

这里我定义了两个静态变量。每个 User 实例都可以访问这两个变量。请注意,我们可以使用 value 对其进行初始化;

Instance functions (methods)

实例函数(方法)

var User = function(name, age) {
    this.name = name;
    this.age = age;
}

User.prototype = {
    getName: function() {
        return this.name;
    },

    setName: function(name) {
        this.name = name;
    }
}

Usage example:

用法示例:

var user = new User('Mike', 29);
user.setName('John');
alert(user.getName()); //should be 'John'

Static functions

静态函数

var User = function(name, age) {
    this.name = name;
    this.age = age;
}

User.create = function(name, age) {
    return new User(name, age);
}

User.prototype = {}

回答by Pointy

Assuming that by "ctor" you mean "constructor", in JavaScript that's just a function. In this case your constructor would need to be "Object1" itself - in other words, what you've got there makes sense if you have already defined "Object1" to be a function.

假设“ctor”的意思是“构造函数”,在 JavaScript 中这只是一个函数。在这种情况下,您的构造函数本身需要是“Object1”——换句话说,如果您已经将“Object1”定义为一个函数,那么您在那里得到的东西是有意义的。

Thus,

因此,

function Object1(param) {
  // constructor code
}

would be the constructor for your type.

将是您的类型的构造函数。

Now there are some JavaScript libraries that provide a utility layer for defining classes. With those, you generally pass some sort of object (like you've got) that includes an "init" function. The libraries provide APIs for creating "classes" and for extending one class from another.

现在有一些 JavaScript 库提供了用于定义类的实用程序层。有了这些,你通常会传递一些包含“init”函数的对象(就像你已经得到的那样)。这些库提供了用于创建“类”和从另一个类扩展一个类的 API。

回答by Emil Ivanov

Javascript has prototype based object model. Check this mozilla wiki pageand suddenly you'll feel much better in js land.

Javascript 具有基于原型的对象模型。检查这个mozilla wiki 页面,突然之间你会在 js 领域感觉好多了。

回答by Kumar Rahul

We can define a constructor in javaScript is same as we fine function, so constructor is just a function.

我们可以在javaScript中定义一个构造函数,就像我们定义的函数一样,所以构造函数只是一个函数。

//function declaration
function func(){}

In case of Contructor We use initial letter in caps in construct like

在构造函数的情况下,我们在构造中使用首字母大写,如

//constructor 
function Func(){}

now do whatever you want to with your constructor

现在对构造函数做任何你想做的事

var constructor1 = new Func();

回答by Muhammad Rehan

class CLASS_NAME
{
   private:
         int variable;
   public:
          CLASS_NAME()    //constructor
          {
                 variable = 0;
          }
};