小编thr*_*eve的帖子

Typescript 编译器不知道类上的 ES6 代理陷阱

我有一个抽象类:

abstract class Foo {
    abstract bar(): string;
}
Run Code Online (Sandbox Code Playgroud)

我有一些扩展的类Foo

class Foo1 extends Foo {
    bar(): string { return 'foo1'; }
}

class Foo2 extends Foo {
    bar(): string { return 'foo2'; }
}
Run Code Online (Sandbox Code Playgroud)

我还有一个类,我要代理的所有方法FooFoo。这实际上工作正常,如果我Foo在这个类上定义所有方法。但我宁愿不这样做。我宁愿让Foo定义的方法Foo和编译器知道FooProxy也实现了这些方法,而不必实际实现它们。这可能吗?Proxy 类看起来像这样:

class FooProxy {
    public foo: Foo;

    constructor(foo: Foo) {
        this.foo = foo;
        let handler = {
            get: function(target: FooProxy, prop: string, receiver: any) {
                if(Foo.prototype[prop] !== null) {
                    return target.foo[prop]; …
Run Code Online (Sandbox Code Playgroud)

typescript es6-proxy angular

10
推荐指数
2
解决办法
7986
查看次数

*ngFor中的双向数据绑定

我创建了一个组件,意在成为一个开关.您可以像使用复选框一样使用它.这是一个精简版.

我-switch.component.ts:

import {Component, Input, Output, EventEmitter} from '@angular/core';

@Component({
    selector: 'my-switch',
    template:    `<a (click)='toggle()'>
                    <span *ngIf='value'>{{onText}}</span>
                    <span *ngIf='!value'>{{offText}}</span>
                  </a>`
})
export class MySwitchComponent {
    @Input() onText: string = 'On';
    @Input() offText: string = 'Off';
    @Input() value: boolean;

    @Output() change = new EventEmitter <boolean> ();

    position: string;

    toggle() {
        this.value = !this.value;
        this.change.emit(this.value);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的计划是这样使用它:

家长component.ts

import {Component} from '@angular/core';
import {MySwitchComponent} from 'my-switch.component';

@Component({
    selector: 'my-sites',
    directives: [MySwitchComponent]
    template: `<table>
                 <tr *ngFor='let item of items'>
                   <td>
                     <my-switch 
                       [(value)]='item.options.option1' …
Run Code Online (Sandbox Code Playgroud)

angular2-template angular

4
推荐指数
1
解决办法
4232
查看次数

Go 中独特函数的集合

我正在尝试在 go 中实现一组功能。上下文是一个事件服务器;我想防止(或至少警告)为一个事件多次添加相同的处理程序。

我读过,地图通常用作集合,因为可以轻松检查成员资格:

if _, ok := set[item]; ok {
    // don't add item
} else {
    // do add item
}
Run Code Online (Sandbox Code Playgroud)

不过,我在使用这种函数范式时遇到了一些麻烦。这是我的第一次尝试:

// this is not the actual signature
type EventResponse func(args interface{})

type EventResponseSet map[*EventResponse]struct{}

func (ers EventResponseSet) Add(r EventResponse) {
    if _, ok := ers[&r]; ok {
        // warn here
        return
    }
    ers[&r] = struct{}{}
}

func (ers EventResponseSet) Remove(r EventResponse) {
    // if key is not there, doesn't matter
    delete(ers, &r)
}
Run Code Online (Sandbox Code Playgroud)

很明显为什么这行不通:函数不是 Go 中的引用类型,尽管有些人会告诉你它们是。 …

pointers set go

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

标签 统计

angular ×2

angular2-template ×1

es6-proxy ×1

go ×1

pointers ×1

set ×1

typescript ×1