3

Im having trouble with the different ways for declaring js-objects, especially after reading the knockout.js documentation. They seem to mix all possible ways. What is the big difference between these four ways of declaring an object?

var object = new Object();
name.field = bla;
name.method = function() { bla };

var object = {
field : "bla",
method : function() { bla };
}

--> I guess these are equivalent, just different notations. But in the second case, how would you pass parameters? Or isnt it possible at all?

And in the two bwlo: Whats the difference between storing it in a variable and not storing it in a variable (other than the way the object would be passed to a function)?

function object() {
this.field = "bla";
this.method = function() { bla };
}

otherfunction(new Object());

var myobject = function name() {
this.field = "bla";
this.method = function() { bla };
}

otherfunction(new myobject());
otherfunction(myobjet);

EDIT: Sorry for asking again, didn't see the other one in the suggested topics.

user2791739
  • 207
  • 1
  • 3
  • 15

1 Answers1

1

In the first one you are using new statement:

The new operator creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

So you can make an object of any function you define

var x = function() {};
new x();

The second thing is an object literal:

An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}).

It's always an instance of an Object function. Why would you like to to pass parameters here? You are defining explicit object, there is no need to pass parameters here.