简体   繁体   English

如何将空的 object 添加到本机构造函数 object

[英]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).我有一个原生 object,在本例中是一个文档 object,它是 Adobe 的 ExtendScript (JavaScript) 原生的。 I want to add an empty object to it, so all document objects have this empty object (property) attached by default.我想向它添加一个空的 object,所以所有文档对象都默认附加了这个空的 object(属性)。 So I don't need to check if the object is undefined , and create it before writing stuff into the object.所以我不需要检查 object 是否undefined ,并在将内容写入 object 之前创建它。 Simply know that this container is always present in a document object.只需知道此容器始终存在于文档 object 中。

Some clarification... In Extendscript there are objects created like document and layer objects.一些澄清......在Extendscript 中创建了像文档和图层对象这样的对象。 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.我想向这个已经存在的构造函数添加原型属性,在这种情况下是一个空的 object,但也可以是生成的 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); arr.remove(3);

This way of adding prototypes works on constructors like arrays.这种添加原型的方式适用于 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.对文件数组 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.在某些情况下,可能没有打开文档,因此在某些情况下运行 app.activeDocument 可能会引发错误。 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...?这家伙为文档 object 做了原型扩展,但没有说先创建文档 object...? https://gist.github.com/DieterHolvoet/ac8130bdf0f0c6c6602b 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.在我处理 object 之前,我不想每次都检查并创建它。

So before adding newProperty as a new object to the document object, I don't want to do...所以在将 newProperty 作为新的 object 添加到文档 object 之前,我不想做...

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...并且只知道所有文档对象都已经创建了这个 object,然后做......

document.newProperty["test"] = 1

Please try this code,To How to add an empty object to a native constructor object请尝试此代码,如何将空的 object 添加到本机构造函数 object

There is no benefit to using new Object();使用 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: {} 语法更短、更整洁(更少 Java 风格),并允许您立即填充 object 内联 - 如下所示:

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

Arrays Arrays

For arrays, there's similarly almost no benefit to ever using new Array();对于 arrays,同样几乎没有使用 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.').创建一个包含未定义的所有插槽的 100 项长数组 - 这在某些情况下可能很好/有用(例如 (new Array(9)).join('Na-Na ') + 'Batman.')。

My recommendation我的推荐

1.Never use new Object(); 1.永远不要使用 new Object(); - it's clunkier than {}; - 它比 {} 更笨重; and looks silly.看起来很傻。 2.Always use []; 2.始终使用[]; - except when you need to quickly create an "empty" array with a predefined length. - 除非您需要快速创建具有预定义长度的“空”数组。

I hope this information will be useful.我希望这些信息会有用。

Thank you.谢谢你。

I think you are mixing up the Class Document and the instances of this document class.我认为您混淆了 Class Document和本文档 class 的实例。

The error you are getting (document is not defined), does not refer to the class Document (that one is defined and already contains your custom added property), but in your snippet the var document (the document instance ) is not defined (at least in cases when there is no active document).您得到的错误(文档未定义),不是指 class Document (该文档已定义并且已经包含您的自定义添加属性),但在您的代码段中,var 文档(文档实例)未定义(在至少在没有活动文档的情况下)。

But you can, without a problem, extend the Document class with custom properties without having a document open, for example this works (test it when there are initially no documents open):但是您可以毫无问题地使用自定义属性扩展 Document class 而无需打开文档,例如这可以工作(在最初没有打开文档时进行测试):

// no documents open, extend Document class with property fruit
// without any additional checking
Document.prototype.fruit = 'banana';

// now create a new document and make use of the custom property you assigned earlier
var newDoc = app.documents.add();
$.writeln(newDoc.fruit); // prints banana

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM