How to add an empty object to a native constructor object

I have a native object, in this case a document object that’s native to Adobe’s ExtendScript (JavaScript). I want to add an empty object to it, so all document objects have this empty object (property) attached by default. So I don’t need to check if the object is undefined, and create it before writing stuff into the object. Simply know that this container is always present in a document object.

Some clarification… In Extendscript there are objects created like document and layer objects. And I would like to add prototype properties to this already existing constructor, in this case an empty object, but can also be other properties to this generated object. A good example I found is a array prototype extension…

    Array.defineProperty(Array.prototype, "remove", {       set: function(){},       get: function(){         return removeArrayElement.bind(this);       }     });     var arr = [0, 1, 2, 3, 4]; arr.remove(3); 

arr.remove(3);

This way of adding prototypes works on constructors like arrays. But when I try this…

Document.prototype.newProperty = new Object  var document = app.activeDocument document.newProperty["test"] = 1 alert(document.newProperty.test) 

It gives an error that document is not defined. Not when doing this to a array of file object. For the document it only works when I first create an document like this…

var tempDocument = app.activeDocument Document.prototype.newProperty = new Object  var document = app.activeDocument document.newProperty["test"] = 1 alert(document.newProperty.test) 

In some situations, there may not be a document open, so running app.activeDocument can trow an error in some situations. Is there a way to not have to create a document first? This guy made prototype extensions for the document object, but didn’t say anything about first creating a document object…? https://gist.github.com/DieterHolvoet/ac8130bdf0f0c6c6602b

In this case it comes down to not having to check if the parent is created before adding to it. I don’t want to check and create it every time if it’s there before I work on the object.

So before adding newProperty as a new object to the document object, I don’t want to do…

if (typeof document.newProperty === "undefined") {     document["newProperty"] = new Object ()     } document.newProperty["test"] = 1 

and just know all document objects have this object already created, and just do…

document.newProperty["test"] = 1 
Add Comment
1 Answer(s)

Please try this code,To How to add an empty object to a native constructor object

There is no benefit to using new Object(); – whereas {}; can make your code more compact, and more readable.

For defining empty objects they’re technically the same. The {} syntax is shorter, neater (less Java-ish), and allows you to instantly populate the object inline – like so:

var myObject = {         title:  'Frog',         url:    '/img/picture.jpg',         width:  300,         height: 200       }; 

Arrays

For arrays, there’s similarly almost no benefit to ever using new Array(); over []; – with one minor exception:

var emptyArray = new Array(100); 

creates a 100 item long array with all slots containing undefined – which may be nice/useful in certain situations (such as (new Array(9)).join(‘Na-Na ‘) + ‘Batman!’).

My recommendation

1.Never use new Object(); – it’s clunkier than {}; and looks silly. 2.Always use []; – except when you need to quickly create an "empty" array with a predefined length.

I hope this information will be useful.

Thank you.

Answered on July 15, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.