object 1:
var vehicle = {
name : "car",
wheels : 4,
fuel : "petrol"
}
object 2:
var vehicle = function(){
this.name = "car",
this.wheels = 4,
this.fuel = "petrol"
}
When both of the objects describe the object vehicle
in the same way, what is the difference between them?
When both of the objects describe the object vehicle in the same way, what is the difference between them?
They don't. Your first vehicle
is an object; vehicle.wheels
is 4
. Your second vehicle
is a function (also an object, but that's not really relevant here); vehicle.wheels
is undefined
because that function doesn't have a wheels
property. You'd have to use var v = new vehicle();
and then v.wheels
.
Use object initializers (your first example) when you need a one-off object. Use a function (like your second example) if you need a factory for objects that have the same shape.
Note that if your factory is a constructor function (a function that expects to be called with new
, such as your vehicle
function), it should be initially capitalized by (overwhelming) convention.
But your factory doesn't have to be a constructor function, it could be a function that just returns an object, in which case you woulnd't use it with new
, wouldn't use this
inside it, and wouldn't typically capitalize it. Eg:
var vehicle = function(){
return {
name: "car",
wheels: 4,
fuel: "petrol"
};
}
(It could use Object.create
if it wanted to assign the object a specific prototype.) Whether you use that and var v = vehicle();
or a constructor function and var v = new Vehicle();
is mostly a matter of style.
The object in JavaScript refers to any entity or a data element. Whereas, a function is a procedure to be performed to achieve something. A function may or may not return an object.
In this case, it looks confusing as they both are used for the same purpose. But, generally this is not how a function is used.
You can create a new instance of this function/class.
var vehicle = function(){
this.name = "car",
this.wheels = 4,
this.fuel = "petrol"
}
Function/Class Objects can be instanciated.
var v1 = new vehicle(); //Here v1 is an instance of vehicle
console.log(v1.name); // Car
In this case you can not create a new instance of this Object. It's just an Object.
var vehicle = {
name : "car",
wheels : 4,
fuel : "petrol"
}
Amrinder is right. With the first example you create an object
var vehicle =
{
name : "car",
wheels : 4,
fuel : "petrol"
}
//vehicle.name shows "car"
If you use option 2 you create a function which you can call to create an object with the given values:
var vehicle2 = function()
{
this.name = "car",
this.wheels = 4,
this.fuel = "petrol"
}
var vehicle = new vehicle2();
//vehicle.name shows "car"
var vehicle = { name : "car", wheels : 4, fuel : "petrol" } typeOf(vehicle) // returns "Object" var vehicle = function(){ this.name = "car", this.wheels = 4, this.fuel = "petrol" } typeof(vehicle) // output "function"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.