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. Wrt line 'var file = new static.Server();' the The author says theres an object static and Server() is a constructor. How is that possible? Constructors are called when a class is instantiated. Does javascript allow objects to call their constructors even after instantiation?
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. Wrt line 'var file = new static.Server();' the The author says theres an object static and Server() is a constructor. How is that possible? Constructors are called when a class is instantiated. Does javascript allow objects to call their constructors even after instantiation?
Share Improve this question asked Sep 9, 2013 at 16:03 user2065736user2065736 1- 7 get out of the class mentality, it will only confuse you in JS. Any function can be constructor, but not all of them are. constructors are not called when a class is created, they are called when they are called, there is no internal init() or self-named builder. – dandavis Commented Sep 9, 2013 at 16:07
3 Answers
Reset to default 10JavaScript doesn't have any classes. It only has objects, and objects are like associative arrays in PHP. For example, consider the following PHP code:
$foo = array(
"bar" => "baz",
"baz" => "bar"
);
In JavaScript you would write it as follows:
var foo = {
bar: "baz",
baz: "bar"
};
The really cool thing about JavaScript however is that it has first-class functions. PHP does not (@$&#%! PHP). This means that functions in JavaScript are simply values. 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.
For example the primary way to create objects in JavaScript is to use functions. A function may be either called normally or be called as a constructor (i.e. by prefixing the function call with the new
keyword). When a function is called as a constructor JavaScript creates a new object behind the scenes and the constructor is used to initialize it. 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
). 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
. However this
will not always be static
depending upon the situation:
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. I suggest you start learning JavaScript from here: https://developer.mozilla/en/learn/javascript
static
is an object with a property called 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
)
theres an object static and Server() is a constructor. How is that possible?
static
is a plain object that has a Server
property which is a function. You can call this function as a constructor with the new
keyword. See static
like a namespace.
Constructors are called when a class is instantiated.
In JavaScript, constructors define a "class" construct. 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?
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.