鉴于此课程; 我将如何迭代它包含的方法?
class Animal {
constructor(type){
this.animalType = type;
}
getAnimalType(){
console.log('this.animalType: ', this.animalType );
}
}
let cat = window.cat = new Animal('cat')
Run Code Online (Sandbox Code Playgroud)
我试过的是以下但没有成功:
for (var each in Object.getPrototypeOf(cat) ){
console.log(each);
}
Run Code Online (Sandbox Code Playgroud) 如何枚举ES6类的方法?相近Object.keys
这是一个例子:
class Callbacks {
method1() {
}
method2() {
}
}
const callbacks = new Callbacks();
callbacks.enumerateMethods(function(method) {
// method1, method2 etc.
});
Run Code Online (Sandbox Code Playgroud) 我有以下课程:
export class Test {
private _rowsCount: string;
public get RowsCount(): string {
return this._rowsCount;
};
public set RowsCount(value: string) {
this._rowsCount = value;
};
private _rowsCount2: string;
public get RowsCount2(): string {
return this._rowsCount2;
};
public set RowsCount2(value: string) {
this._rowsCount2 = value;
};
}
Run Code Online (Sandbox Code Playgroud)
我需要迭代特定类中的属性,我尝试了以下内容:
Object.keys(this).forEach((key)=> {
console.log(key);
});
Run Code Online (Sandbox Code Playgroud)
但是这个问题只是在private字段上进行迭代,我也尝试了以下我得到了所有的methods和properties:
for (var property in this) {
if (this.hasOwnProperty(property)) {
console.log(property);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有人有办法解决吗?
谢谢!
我厌倦了编写这样的代码:
class Something {
constructor() {
this.method = this.method.bind(this);
this.anotherOne = this.anotherOne.bind(this);
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
很费时间,而且很容易忘记绑定一个方法。我知道 class fields 提案,但它仍然是第 3 阶段,似乎有一些问题。
我当前的解决方案(基于此答案)如下所示:
class Something {
constructor() {
// Get all defined class methods
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(this));
// Bind all methods
methods
.filter(method => (method !== 'constructor'))
.forEach((method) => { this[method] = this[method].bind(this); });
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但我想知道是否有更好的方法,或者此方法是否存在我不知道的问题。
我遇到的问题是,如果我没有在构造函数中绑定我的类函数,我必须记住以后“正确地”调用它们。例如:
const classInstance = new Something();
// This fails for a non-obvious reason
someAction()
.then(classInstance.method);
// This …Run Code Online (Sandbox Code Playgroud) 鉴于这两个类
class Foo{
f1;
get f2(){
return "a";
}
}
class Bar extends Foo {
b1;
get b2(){
return "a";
}
}
let bar = new Bar();
Run Code Online (Sandbox Code Playgroud)
什么代码会从bar实例中获取此属性列表?['f1', 'f2', 'b1', 'b2']
更新
这应该是@Marc C答案的一部分:
使用装饰器我可以轻松地将不可枚举的属性转换为可枚举的属性:
class Bar extends Foo {
@enumerable()
get b2(){
return "a";
}
}
Run Code Online (Sandbox Code Playgroud)
这是装饰源:
function enumerable() {
return function(target, key, descriptor) {
if (descriptor) {
descriptor.enumerable = true;
}
};
}
Run Code Online (Sandbox Code Playgroud)
我需要复制一个对象及其方法.因此,我对该对象进行字符串化,然后解析它并从原始对象添加方法(但绑定到此新副本).
// Taken from: https://stackoverflow.com/questions/31054910/get-functions-methods-of-a-class
function getAllMethods(obj) {
let props = [];
do {
const l = Object.getOwnPropertyNames(obj)
.concat(Object.getOwnPropertySymbols(obj).map(s => s.toString()))
.sort()
.filter((p, i, arr) =>
typeof obj[p] === 'function' && //only the methods
p !== 'constructor' && //not the constructor
(i == 0 || p !== arr[i - 1]) && //not overriding in this prototype
props.indexOf(p) === -1 //not overridden in a child
)
props = props.concat(l)
}
while (
(obj = Object.getPrototypeOf(obj)) && //walk-up the prototype chain
Object.getPrototypeOf(obj) …Run Code Online (Sandbox Code Playgroud) 为了构建我的JavaScript代码,我想使用类,ES6的新原型继承的语法糖.我遇到了麻烦:似乎我的原型方法没有按预期工作,这让我觉得我可能对JavaScript类有一个基本的误解,和/或它们是如何工作的.
采用以下代码,声明传统构造函数Cat并修改其原型,以及Dog在其中定义的原型方法的ES6类.
// Traditional Cat Constructor
// ===========================
function Cat(name) {
this.name = name;
this.sound = "Meow.";
}
Cat.prototype.speak = function() {
console.log(this.sound || "I don't make a sound.");
};
// Dog via ES6 Class
// =================
class Dog {
constructor(name) {
this.name = name;
this.sound = "Woof!";
}
speak() {
console.log(this.sound);
}
}
var cat = new Cat("Bella");
var dog = new Dog("Sparky");
Run Code Online (Sandbox Code Playgroud)
在NodeJS中(使用4.4.2和6.0.0-pre测试),当我尝试获取实例的原型时Dog,我得到一个空对象,但我仍然能够在实例上调用prototype方法.
Object.getPrototypeOf(cat);
// -> Cat { …Run Code Online (Sandbox Code Playgroud) 例如,我有一些类有一些方法:
class SomeClass() {
someMethod1() {
}
someMethod2() {
}
}
Run Code Online (Sandbox Code Playgroud)
我可以获取此类实例的所有函数名称吗?
Object.keys(new SomeClass())
Run Code Online (Sandbox Code Playgroud)
给了我一个空数组.
我想要像数组一样['someMethod1', 'someMethod2'].
javascript ×8
ecmascript-6 ×5
arrays ×1
class ×1
es6-class ×1
function ×1
methods ×1
node.js ×1
object ×1
typescript ×1