0

如果我使用(在文本框架上):

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

然后它按预期工作,这意味着所选对象有一个 fit 方法。

如果我使用:

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

在同一个选择中,它不会打印 fit 方法。

如果我使用这个:

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()));

然后我也没有得到 fit 方法。我真的很想知道所选对象的所有方法和属性。那么我该如何得到它们呢?

4

3 回答 3

2

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

于 2014-11-12T23:47:10.913 回答
1

或者只是使用b.inspect(obj). 以递归方式将对象的所有属性和值打印到控制台。见http://basiljs.ch/reference/#inspect

于 2014-11-12T14:23:24.607 回答
1

当方法fit()存在并且不发光时,for-in-loop它是一个不可枚举的属性。

有不同的方法可以访问对象的属性:

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
});

如果您没有.fit()在其中一种方式上找到它的不可枚举且不是 obj 的 OWN属性,而是位于obj原型中的某个位置。然后你可以这样做:

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

对象通常有更长的原型链,而属性位于更深的位置。然后,您只需根据需要重复最后一个片段:

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

为了使它完整:假设您.fit在 obj 的原型上找到了prot。然后你可以检查它:

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

这会输出一个对象,该对象显示其以及prot.fit它是否可枚举、可写和可配置。Object.methods 和更多的FIND HERE

于 2014-11-12T13:39:03.553 回答