使用装饰器包装类的过程会导致超类无法访问该类的属性.为什么?
我有一些代码:
这是代码:
function wrap(target: any) {
// the new constructor
var f: any = function (...args) {
return new target();
}
f.prototype = target.prototype;
return f;
}
@wrap
class Base {
prop: number = 5;
}
class Extended extends Base {
constructor() {
super()
}
}
var a = new Extended()
console.log(new Extended().prop) // I'm expecting 5 here, but I get undefined.
Run Code Online (Sandbox Code Playgroud)
我确信这是一般原型的一些细微差别,或者是TypeScript处理它们的具体方式,我没有掌握.
var subimage = new Bitmap();
subimage.bitmapData = new BitmapData(25, 25, true, 0);
addChild(subimage);
Run Code Online (Sandbox Code Playgroud)
从我读过的所有内容来看,这应该是透明的.我看到一个大黑方块.可能导致什么?
通常我会使用类似的东西str[i].
但如果str = "??"呢?
str[i]失败.for (x of str) console.log(x)也失败了.它打印出总共4个字符,即使字符串中只有2个表情符号.
什么是迭代我能在字符串中看到的每个字符的最佳方式(和新行,我猜),没有别的?
理想的解决方案将返回2个字符的数组:2个表情符号,没有别的.声明的副本和我发现的一堆其他解决方案不符合此标准.
我有这段JavaScript代码:
class Foo {
constructor() {
this.b = 1;
this.getB = () => { return this.b; };
}
}
const normalFoo = new Foo();
const clonedFoo = magicClone(normalFoo);
clonedFoo.b = 5;
console.log(clonedFoo instanceof Foo); // should be true
console.log(clonedFoo.getB()); // should be 5
Run Code Online (Sandbox Code Playgroud)
我想知道我可以替换什么magicClone以获得所需的结果(例如,一个尊重箭头功能绑定的克隆).
我对任何类型的可怕黑客都很好,只要他们在这种情况下工作,我也可以使用大多数时间都有效的解决方案.这主要是为了我的启发:)
请不要将此问题作为副本关闭 - 克隆对象已被多次询问,但我找不到单一的答案.Object.assign,lodash cloneDeep,jQuery的克隆等都不处理这种情况.
尝试在Google Chrome浏览器中调试TypeScript应用程序时,* .ts文件在检查器中已正确映射到源,但是* .tsx文件显得完全为空,这实际上并不能帮助我调试它们。
我怎样才能* .tsx文件正确地映射到源地图?
我在网站中间点击一下,代码如下 <a href=“#“ onclick=“…
该功能运行良好,但是当我点击链接时,a href ="#"让页面总是跳到顶部.它有什么办法吗?
谢谢
通常我会使用Type.getClassName(Type.getClass(this),但显然这不起作用,因为没有this.有任何想法吗?
在Typescript中,我经常发现自己编写这种getter/setter样板:
private _length = 0;
get length(): number { return this._length; }
set length(value: number) { this._length = value; }
Run Code Online (Sandbox Code Playgroud)
在C#中,我知道你可以写这样的东西:
public length { get; set; }
Run Code Online (Sandbox Code Playgroud)
是否有相同的方法来简化我的Typescript代码?