相关疑难解决方法(0)

ES6迭代类方法

鉴于此课程; 我将如何迭代它包含的方法?

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)

javascript ecmascript-6

34
推荐指数
5
解决办法
1万
查看次数

如何枚举es6类方法

如何枚举ES6类的方法?相近Object.keys

这是一个例子:

class Callbacks {
  method1() {
  }

  method2() {
  }
}

const callbacks = new Callbacks();

callbacks.enumerateMethods(function(method) {
  // method1, method2 etc.
});
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-6

34
推荐指数
1
解决办法
1万
查看次数

在typescript中获取类的属性

我有以下课程:

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字段上进行迭代,我也尝试了以下我得到了所有的methodsproperties:

    for (var property in this) {
        if (this.hasOwnProperty(property)) {
            console.log(property);                
        }
    }
Run Code Online (Sandbox Code Playgroud)

有没有人有办法解决吗?

谢谢!

javascript typescript

13
推荐指数
1
解决办法
1万
查看次数

什么是自动绑定JS类方法的好方法?

我厌倦了编写这样的代码:

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)

javascript arrays function object ecmascript-6

10
推荐指数
3
解决办法
5755
查看次数

如何迭代ES6/2015类实例的属性

鉴于这两个类

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)

这是一个巴别样本

javascript ecmascript-next

5
推荐指数
1
解决办法
1230
查看次数

引用复制对象时出现问题

我需要复制一个对象及其方法.因此,我对该对象进行字符串化,然后解析它并从原始对象添加方法(但绑定到此新副本).

// 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

3
推荐指数
1
解决办法
32
查看次数

为什么我的班级原型不起作用?

为了构建我的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)

javascript node.js ecmascript-6

2
推荐指数
1
解决办法
1185
查看次数

如何获取es6类实例的所有函数

例如,我有一些类有一些方法:

class SomeClass() {
    someMethod1() {
    }
    someMethod2() {
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以获取此类实例的所有函数名称吗?

Object.keys(new SomeClass()) 
Run Code Online (Sandbox Code Playgroud)

给了我一个空数组.

我想要像数组一样['someMethod1', 'someMethod2'].

javascript methods class ecmascript-6 es6-class

2
推荐指数
1
解决办法
2189
查看次数