function Dog(name) {
if (name) { this.name = name; }
this.speak = function() {
return this.name + " says woof";
}
}
var v = new Dog("Vincent");
- Create an empty object;
- Set the new object's prototype to point to the Dog.prototype;
- And call the constructor method on the new object.
var v = Object.create(Dog.prototype);
v.constructor("Vincent");
v.speak(); // "Vincent says woof"
The first line above creates an empty object and sets its prototype to equal the first argument, in this case Dog.prototype. The second line simply executes the constructor method on the object, which now points to the Dog function.