As per the information provided in this MSDN article - specifically focusing on
Constructor Functions but No Classes
, (and after consulting the MDN JS reference) I attempted to create an object using the following code:
function Dog(name){
this.name = name;
}
// EXAMPLE 1
var dog = new Dog("Spot");
console.log("Dog using new:");
console.log(dog); // Instance of Dog object is displayed
// EXAMPLE 2
var dog = {};
dog = Dog.call(dog,"Rowdie");
console.log("Dog using call:");
console.log(dog); // Unexpectedly returns Undefined.. why is that?
Despite the fact that the first example (the conventional method for creating a new object) yields the desired instance, the second example produces undefined
- am I missing something here?
For testing purposes, you can refer to the JSFiddle link: http://jsfiddle.net/wk8JD/1/