简体   繁体   English

获取对象的所有方法和属性

[英]get all methods and properties of an object

If I use (on a text frame): 如果我使用(在文本框架上):

b.selection().fit(FitOptions.frameToContent);

Then it works as expected, this means that the selected object has a fit method. 然后它将按预期工作,这意味着所选对象具有fit方法。

If I use: 如果我使用:

for (var property in b.selection()) {
    b.println(property);
}

On the same selection then it doesn't print a fit method. 在同一选择上,则不会打印出拟合方法。

If I use this: 如果我使用这个:

function getMethods(obj) {
  var result = [];
  for (var id in obj) {
    try {
      if (typeof(obj[id]) == "function") {
        result.push(id + ": " + obj[id].toString());
      }
    } catch (err) {
      result.push(id + ": inaccessible");
    }
  }
  return result;
}


b.println(getMethods(b.selection()));

Then I don't get the fit method either. 然后我也没有fit方法。 And I would really like to know all methods and properties of the selected object. 我真的很想知道所选对象的所有方法和属性。 So how do i get them? 那我怎么得到它们呢?

When method fit() exists and doesn't shine up in the for-in-loop it is a non-enumerable property. 当方法fit()存在并且在for-in-loop中不发光时,它是不可枚举的属性。

There are different ways to access the properties of an object: 有多种访问对象属性的方法:

var obj = b.selection();
for (var p in obj) {
    console.log(p); // --> all enumerable properties of obj AND its prototype
}
Object.keys(obj).forEach(function(p) {
    console.log(p); // --> all enumerable OWN properties of obj, NOT of its prototype
});
Object.getOwnPropertyNames(obj).forEach(function(p) {
    console.log(p); // all enumerable AND non-enumerable OWN properties of obj, NOT of its prototype
});

If you don't find .fit() on one of this ways its not enumerable AND not OWN property of obj, but sits somewhere in the prototype of obj. 如果您找不到以这种方式之一的.fit() ,它不是 obj的不可枚举且 不是OWN属性,而是位于obj 原型中的某个位置。 Then you can do: 然后,您可以执行以下操作:

var prot = Object.getPrototypeOf(obj);
Object.getOwnPropertyNames(prot).forEach(function(pp) {
    console.log(pp); // --> all enumerable AND non-enumerable properties of obj's prototype
});

Often objects have a bit longer prototype-chain and the property sits somewhere deeper on it. 对象通常具有更长的原型链,并且该属性位于其上的某个位置。 Then you just repeat the last snippet as often as needed: 然后,您只需根据需要重复最后一个片段:

var prot2 = Object.getPrototypeOf(prot);
Object.getOwnPropertyNames(prot2).forEach( /*...*/ );

To make it complete: Let's say you have found .fit on the obj's prototype prot . 要使其完整:假设您在obj的原型prot上找到了.fit Then you can inspect it: 然后您可以检查它:

console.log(Object.getOwnPropertyDescriptor(prot.fit));

That outputs an object which shows the value of prot.fit and whether it's enumerable, writable, and configurable or not. 输出的对象显示prot.fit以及它是否可枚举,可写和可配置 The Object.methods and some more FIND HERE . 该Object.methods和一些在这里找到

Or just use b.inspect(obj) . 或者只是使用b.inspect(obj) Prints out all properties and values of an object in a recursive manner to the console. 以递归方式将对象的所有属性和值打印到控制台。 See http://basiljs.ch/reference/#inspect 参见http://basiljs.ch/reference/#inspect

尝试obj.reflect.methods获取所有方法

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

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