最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Creating instances of predefined objects in Javascript - Stack Overflow

programmeradmin5浏览0评论

I know that in Javascript we can create ,instatnces of object like

var ins = new myObject();

I know that ,window ,document etc are predefined objects in javascript..Can we create new instances of these objects.? For ex:
Is

var inss = new document();

possible?

I know that in Javascript we can create ,instatnces of object like

var ins = new myObject();

I know that ,window ,document etc are predefined objects in javascript..Can we create new instances of these objects.? For ex:
Is

var inss = new document();

possible?

Share Improve this question asked Apr 30, 2012 at 12:41 Jinu Joseph DanielJinu Joseph Daniel 6,31117 gold badges64 silver badges93 bronze badges 2
  • 1 You can simulate your own constructor functions for DOM Elements. Take jsdom for example. – Aadit M Shah Commented Apr 30, 2012 at 12:45
  • I dont think so. Trying this var inss = new document(); on console give error "TypeError: object is not a function" – Adil Commented Apr 30, 2012 at 12:54
Add a ment  | 

5 Answers 5

Reset to default 3

Don't confuse objects with constructors (or classes in most OOP languages). In JavaScript, you create objects by calling constructor functions using the new operator:

function MyObject()
{
}

var obj = new MyObject();

Afterwards you can access the constructor given the object using the constructor property:

var ctor = obj.constructor;  // (ctor === MyObject) will be true

Theoretically, you can create new objects of the same type as a given object:

var obj1 = new MyObject();
var obj2 = new obj1.constructor();

In your case, you might try the same with "built-in" object, but it will probably not work since the script engine might forbid it. For example, Chrome will throw TypeError: Illegal constructor when trying to create a new document using new document.constructor(). This is because document's constructor, HTMLDocument, is not meant to be used directly.

Yes and no, mostly no.

You can create a new window object using window.open. It will also have a new document object.

You can create a new DOM document via createDocument, though it won't necessarily have all the special features of the pre-made one. You can also create a new document fragment via createDocumentFragment, which can be very handy.

No, you can't. Although most of these host objects have constructors (e.g. HTMLDocument for document), they are only used for inheritance feautures (e.g. the instanceof operator) but can not be invoked.

> document.constructor
HTMLDocument
> new HTMLDocument
Unhandled DOMException: NOT_SUPPORTED_ERR

You also can't create Nodes for example, these "constructors" are just interfaces.

Yet, you can create a new DOM with the createDocument method, which is available at the document.implementation object.

The new operator only works with objects that are user defined, or built-ins that have a constructor. document and window don't have constructors.

document is not a constructor, it's a constructed object. What you are trying to do is like saying new new Object() or new {}.

The constructor of document is HTMLDocument but you cannot construct it that way, you must use document.implementation.createDocument()

发布评论

评论列表(0)

  1. 暂无评论