Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Constructing Objects

Tags:

javascript

I'm a bit of a javascript noob, but I have a general question that has been bothering me. Lets take an example:

If I hop into a JS console on Chrome, I can easily browse the window object. window seems to list a lot 'class types', so to speak. For example, window.CSSPrimitiveValue is one such property. When I evaluate window.CSSPrimitiveValue in the console, I get back what looks like a constructor (possibly):

function CSSPrimitiveValue() { [native code] }

So it looks like the function was implemented in some native language. No matter. Naturally, I want to construct one of these things, like so:

var test = new CSSPrimitiveValue();

But then I get an error:

TypeError: Illegal constructor

I suspect that either I am calling the constructor incorrectly, or it isn't a constructor at all. I'm a pretty big JS noob about this, but is there any way for me to manually construct one of these objects? What would be the method to going about that?

like image 545
Ben Avatar asked Mar 30 '12 21:03

Ben


People also ask

How can we create objects in JavaScript?

Creating a JavaScript ObjectCreate a single object, using an object literal. Create a single object, with the keyword new . Define an object constructor, and then create objects of the constructed type. Create an object using Object.create() .

What is object constructor in JavaScript?

The Object constructor turns the input into an object. Its behavior depends on the input's type. If the value is null or undefined , it will create and return an empty object. Otherwise, it will return an object of a Type that corresponds to the given value. If the value is an object already, it will return the value.

What are JavaScript objects?

In JavaScript, an object is a standalone entity, with properties and type. Compare it with a cup, for example. A cup is an object, with properties. A cup has a color, a design, weight, a material it is made of, etc. The same way, JavaScript objects can have properties, which define their characteristics.

Can we create object of function in JavaScript?

We then simple create an object obj of the vehicle, initialize it and call it's method. Output: In the above code we created a simple object named car with the help of object literal,having properties like name,maker,engine. Then we make use of the property accessor methods(Dot notation,Bracket notation) to console.


1 Answers

Any function that is written in JavaScript can be a constructor when called using the new keyword. As you already noticed the function you are dealing with is native, i.e. written in C or C++ (probably C++ since Chrome's JavaScript engine is written in C++, too). Native functions/objects can have specific behaviour such as your case where you cannot use it as a constructor - there's nothing you can do.

That "function" doesn't even have a .call() method, you you also cannot call it on an object you created before (not that it would be very useful as it wouldn't have the proper [[Prototype]] set)

like image 52
ThiefMaster Avatar answered Nov 02 '22 06:11

ThiefMaster