Javascript ES6检索类的成员
其思想是获取类的方法/属性列表。例如,我有一门课:Javascript ES6检索类的成员,javascript,class,reflection,ecmascript-6,Javascript,Class,Reflection,Ecmascript 6,其思想是获取类的方法/属性列表。例如,我有一门课: // foo.js class FooController { constructor() { } *bar(next) { yield next; return 'meh'; } //, meh() { return 'bar'; } } module.exports = FooController; 现在我想检索FooControlle
// foo.js
class FooController {
constructor() {
}
*bar(next) {
yield next;
return 'meh';
}
//,
meh() {
return 'bar';
}
}
module.exports = FooController;
现在我想检索FooController
的成员。对于javascript普通对象,这应该很容易做到,但对于类,则不必这么做:
// index.js
var Foo = require('foo');
var foo = new Foo();
// inspect
console.log(foo); // expected: { bar, meh }, but got {}
// struggle with lodash
var _ = require('lodash');
console.log(_.keys(foo)); // expected ['bar', 'meh'], but got []
有什么想法吗?提前感谢您的帮助或建议。选项1:您可以打印
类构造函数
,它会给出所需的结果:
class FooController {
constructor() {
}
*bar(next) {
yield next;
return 'meh';
}
meh() {
return 'bar';
}
}
var foo = new FooController();
console.log(foo.constructor);
function class FooController {
constructor() {
}
*bar(next) {
yield next;
return 'meh';
}
meh() {
return 'bar';
}
}
选项2:您可以只打印类对象
,而不绑定它:
class FooController {
constructor() {
}
*bar(next) {
yield next;
return 'meh';
}
meh() {
return 'bar';
}
}
console.log(FooController);
function class FooController {
constructor() {
}
*bar(next) {
yield next;
return 'meh';
}
meh() {
return 'bar';
}
}
选项1:您可以打印
类构造函数
,它会给出所需的结果:
class FooController {
constructor() {
}
*bar(next) {
yield next;
return 'meh';
}
meh() {
return 'bar';
}
}
var foo = new FooController();
console.log(foo.constructor);
function class FooController {
constructor() {
}
*bar(next) {
yield next;
return 'meh';
}
meh() {
return 'bar';
}
}
选项2:您可以只打印类对象
,而不绑定它:
class FooController {
constructor() {
}
*bar(next) {
yield next;
return 'meh';
}
meh() {
return 'bar';
}
}
console.log(FooController);
function class FooController {
constructor() {
}
*bar(next) {
yield next;
return 'meh';
}
meh() {
return 'bar';
}
}
您应该查询
Foo()
类实例的prototype
对象:
// index.js
var Foo = require('foo');
var foo = new Foo();
var _ = require('lodash');
console.log(Object.getOwnPropertyNames((Object.getPrototypeOf(foo)))); // shows ['bar', 'meh', 'constructor']
// or
console.log(Object.getOwnPropertyNames(foo.__proto__)); // shows ['bar', 'meh', 'constructor']
建议的.keys(foo)
只返回对象所拥有的属性(bar()
和meh()
被继承),实际上是空的[]
要检索所需的方法,请执行以下操作:
- 访问实例的
对象prototype
- 枚举
原型的属性。由于所需的属性不可枚举(
或Object.keys()
将不起作用),因此有必要使用.keys()
Object.getOwnPropertyNames()
有关完整示例,请参见。您应该查询
Foo()
类实例的prototype
对象:
// index.js
var Foo = require('foo');
var foo = new Foo();
var _ = require('lodash');
console.log(Object.getOwnPropertyNames((Object.getPrototypeOf(foo)))); // shows ['bar', 'meh', 'constructor']
// or
console.log(Object.getOwnPropertyNames(foo.__proto__)); // shows ['bar', 'meh', 'constructor']
建议的.keys(foo)
只返回对象所拥有的属性(bar()
和meh()
被继承),实际上是空的[]
要检索所需的方法,请执行以下操作:
- 访问实例的
对象prototype
- 枚举
原型的属性。由于所需的属性不可枚举(
或Object.keys()
将不起作用),因此有必要使用.keys()
Object.getOwnPropertyNames()
有关完整示例,请参见。
类
ES6中的es只是在ES5中函数的原型
上定义方法的糖码。以你为例:
class FooController {
constructor() {
}
*bar(next) {
yield next;
return 'meh';
}
//,
meh() {
return 'bar';
}
}
module.exports = FooController;
ES5等效标准为:
var FooController = (function() {
function FooController() { } // This method will act as our constructor
// All methods defined on the prototype are equivalent with the
// so called class methods in ES6
FooController.prototype.bar = *function(next) { yield next; return 'meh'; };
FooController.prototype.meh = function() { return 'bar'; };
return FooController;
})();
要访问offFooController
的“公共”方法,您可以:
FooController.prototype
console.log(FooController.prototype.meh())/=>bar'
var foo=新的FooController();
变量proto=foo.\uuuu proto\uuuuu;
console.log(proto.meh())/=>bar'
foo
调用new
关键字时,除其他外,以下几个步骤将发生:
FooController
的原型李>
只要你把类方法
看作是糖编码而不是原型
方法,那么在这种情况下就更容易获得力量
希望这能有所帮助。
类
ES6中的es只是对ES5中函数的原型
上定义方法的糖衣编码。以你为例:
class FooController {
constructor() {
}
*bar(next) {
yield next;
return 'meh';
}
//,
meh() {
return 'bar';
}
}
module.exports = FooController;
ES5等效标准为:
var FooController = (function() {
function FooController() { } // This method will act as our constructor
// All methods defined on the prototype are equivalent with the
// so called class methods in ES6
FooController.prototype.bar = *function(next) { yield next; return 'meh'; };
FooController.prototype.meh = function() { return 'bar'; };
return FooController;
})();
要访问offFooController
的“公共”方法,您可以:
FooController.prototype
console.log(FooController.prototype.meh())/=>bar'
var foo=新的FooController();
变量proto=foo.\uuuu proto\uuuuu;
console.log(proto.meh())/=>bar'
foo
调用new
关键字时,除其他外,以下几个步骤将发生:
FooController
的原型李>
只要你把类方法
看作是糖编码而不是原型
方法,那么在这种情况下就更容易获得力量
希望这有帮助。在*条后面的逗号是故意的吗?我发现了一个语法错误。哦,对不起,这是打字错误——让我编辑一下,谢谢你的更正!在*条后面的逗号是故意的吗?我发现了一个语法错误。哦,对不起,这是打字错误——让我编辑一下,谢谢你的更正!问题是如何检索成员,而不是如何打印蓝图。是的,@zeroflagL是对的!我想要的是成员而不是蓝图。问题是如何检索成员,而不是如何打印蓝图。是的,@zeroflagL是对的!我希望大家不要蓝图。在我这边,它显示的是
[]
,而不是['bar','meh']
@Adiono-Oops。答案更新。酷!现在开始工作了,谢谢你,德米特里!在我的末尾,它显示的是[]
,而不是['bar','meh']
@adionooops。答案更新。酷!现在开始工作了,谢谢你,德米特里!它不工作,在两个方向上都显示[]
(请参阅:)。我想我将使用@Dmitri Pavlutin的答案。meh
和bar
不是可枚举属性。这就是为什么Object.keys()
不起作用的原因。是的,Dimitri是对的。我将编辑我的答案。您可以直接从原型中访问这些方法,不能使用keys()
方法列出它们。啊,我现在看到了,很好的解释!谢谢你,安德烈!它不工作,在两个方向上都显示[]
(请参阅:)。我想我将使用@Dmitri Pavlutin的答案。meh
和bar
不是可枚举属性。这就是为什么Object.keys()
不起作用的原因。是的,Dimitri是对的。我将编辑我的答案。您可以直接从原型访问这些方法,但不能使用keys()列出它们