2

Why I can't create a new instance of window.

When I try:

var mywin = new window();

it throws:

TypeError: object is not a function

I guess window is a static object, is there any way of converting static objects to dynamic so that I can do something like this:

window.prototype.something = 'value'; 

Could someone shed some light on this please?

RuntimeException
  • 1,135
  • 2
  • 11
  • 25

3 Answers3

5

window is a special object representing the global context in Javascript. It is an instance of the Window class, but you cannot create instances of that class, as it has some special significance to the interpreter.

(If you just want to create a window, that can be done using the window.open() function.)

1

window is not a class, it's a reference to the current Window object.

A Window object represents a window in the browser, so you can't just create an instance of it. You use the open method to open a new window, thereby creating a new Window instance:

var mywin = window.open('http://www.guffa.com', '_blank');

The _blank target makes the open call actually open a new window, instead of opening a page in the current window.

Window is not a class, it's an interface, so it doesn't have a prototype. You can extend the current window with properties, but that will only affect that instance, not all instances:

window.something = 'value';
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
-2

The error message is self explanatory! window is not a function, you only use the new keyword before a function and then this function is called a constructor function. And what new keyword does, is create a new object and set it as the context of the constructor function.

tikider
  • 540
  • 3
  • 12