简体   繁体   English

Node.js中Javascript的新运算符

[英]Javascript's new operator in node.js

I have the following code: 我有以下代码:

    var http = require('http');
    var static = require('node-static');
    var file = new static.Server();
    http.createServer(function (req, res) {
    file.serve(req, res);
    }).listen(1337, '127.0.0.1');
    console.log('Server running at http://127.0.0.1:1337/');

from the book, node.js for php developers. 从本书中,适用于php开发人员的node.js。 Wrt line 'var file = new static.Server();' WRT行'var file = new static.Server();' the The author says theres an object static and Server() is a constructor. 作者说有一个静态对象,而Server()是构造函数。 How is that possible? 那怎么可能? Constructors are called when a class is instantiated. 实例化类时调用构造函数。 Does javascript allow objects to call their constructors even after instantiation? javascript是否即使在实例化之后也允许对象调用其构造函数?

JavaScript doesn't have any classes. JavaScript没有任何类。 It only has objects, and objects are like associative arrays in PHP. 它只有对象,对象就像PHP中的关联数组。 For example, consider the following PHP code: 例如,考虑以下PHP代码:

$foo = array(
    "bar" => "baz",
    "baz" => "bar"
);

In JavaScript you would write it as follows: 在JavaScript中,您可以这样编写:

var foo = {
    bar: "baz",
    baz: "bar"
};

The really cool thing about JavaScript however is that it has first-class functions. 但是,关于JavaScript的真正酷的地方是它具有一流的功能。 PHP does not (@$&#%! PHP). PHP不支持(@ $&#%!PHP)。 This means that functions in JavaScript are simply values. 这意味着JavaScript中的函数只是值。 You can store them in variables, pass them around as parameters and return values and even put them on objects. 您可以将它们存储在变量中,将它们作为参数传递并返回值,甚至将它们放在对象上。 For example: 例如:

var object = {
    add: function (x, y) {
        return x + y;
    }
};

object.add(2, 3); // 5

Interestingly JavaScript is an amalgamation of both functional and object-oriented programming styles and these two styles of programming are interwoven into the fabric of the language so intricately that it's impossible to separate them. 有趣的是,JavaScript是功能和面向对象的编程风格的结合体,这两种编程风格错综复杂地交织到语言的结构中,无法将它们分开。

For example the primary way to create objects in JavaScript is to use functions. 例如,在JavaScript中创建对象的主要方式是使用函数。 A function may be either called normally or be called as a constructor (ie by prefixing the function call with the new keyword). 一个函数可以被正常调用,也可以被称为构造函数(即,在函数调用之前添加new关键字)。 When a function is called as a constructor JavaScript creates a new object behind the scenes and the constructor is used to initialize it. 当一个函数被称为构造函数时,JavaScript在幕后创建一个新对象,并使用构造函数对其进行初始化。 For example: 例如:

function Person(firstname, lastname) {
    this.fullname = firstname + " " + lastname;
}

var me = new Person("Aadit", "Shah");
alert(me.fullname); // Aadit Shah

In your case the function Server is a property of the object static (which effectively makes the function Server a method of static ). 在您的情况下,功能Server是对象static的属性(这实际上使功能Serverstatic的方法)。 Like methods in other programming languages we call it as follows: 与其他编程语言中的方法一样,我们将其称为:

static.Server();

In this case the special variable this inside the function Server will point to the object static because it's called as a method of static . 在这种情况下, Server函数中的this特殊变量将指向static对象,因为它被称为static方法。 However this will not always be static depending upon the situation: 但是,根据情况, this并不总是static的:

var o = {};
var Server = static.Server;

Static();                   // this will be the global object
Static.call(o);             // this will be the object o
new static.Server;          // this will be the newly created object

As you can see JavaScript is a very dynamic and versatile language, and it's much more powerful than PHP. 正如你所看到的JavaScript是一个非常有活力和灵活的语言,它比PHP 更加强大。 I suggest you start learning JavaScript from here: https://developer.mozilla.org/en/learn/javascript 我建议您从这里开始学习JavaScript: https//developer.mozilla.org/en/learn/javascript

static is an object with a property called Server . static是具有称为Server的属性的对象。
The value of this property is a function. 此属性的值是一个函数。

Your code calls that function as a constructor. 您的代码将其作为构造函数调用。

static itself is probably a plain object (its constructor is probably Object ) static本身可能是一个普通对象(其构造函数可能是Object

theres an object static and Server() is a constructor. 有一个静态对象,Server()是一个构造函数。 How is that possible? 那怎么可能?

static is a plain object that has a Server property which is a function. static是一个普通对象,具有一个Server函数,该函数是一个函数。 You can call this function as a constructor with the new keyword . 您可以使用new关键字将此函数作为构造函数调用。 See static like a namespace. 看到static就像一个名称空间。

Constructors are called when a class is instantiated. 实例化类时调用构造函数。

In JavaScript, constructors define a "class" construct . 在JavaScript中, 构造函数定义“类”构造 Therefore, it should be "A constructor is called to instantiate an object (with the respective prototype)". 因此,应为“调用构造函数以实例化对象(具有相应的原型)”。

Does javascript allow objects to call their constructors even after instantiation? javascript是否即使在实例化之后也允许对象调用其构造函数?

Technically, yes. 从技术上讲,是的。 A construction is always "create a new object (with inheritance etc), then call the constructor on it to initialise the object", so that's no news. 构造总是“创建一个新的对象(带有继承等),然后在其上调用构造函数以初始化该对象”,所以这不是新闻。 Yet, in JavaScript you can apply [custom, non-native] constructor functions on every arbitrary object using the .call method . 但是,在JavaScript中,您可以使用.call方法将[custom,non-native]构造函数应用于每个任意对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM