我认为这是一个范围问题,但我不知道如何解决这个问题.这是我的代码:http://jsfiddle.net/9k9Pe/1498/
class FrameCreator{
constructor(){
this.createFrame();
}
createFrame(){
var iframe = document.createElement('iframe');
this.iframe = iframe;
var frameLoaded=this.frameLoaded;
iframe.onload = function () {
frameLoaded();
};
document.body.appendChild(iframe);
}
frameLoaded(){
console.log("frame loaded");
}
}
class CustomFrameCreator extends FrameCreator{
addContent(){
console.log(this); // returns the object
}
frameLoaded(){
console.log(this); // returns undefined
}
}
var frame=new CustomFrameCreator();
frame.addContent();
Run Code Online (Sandbox Code Playgroud)
frameLoaded()打印undefined,同时addContent打印对象.
如何解决这个问题,所以我可以在加载框架时有一个参考?
谢谢
假设我有这个类(我使用它像枚举):
class Color {
static get Red() {
return 0;
}
static get Black() {
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么类似Object.keys拿到['Red', 'Black']?
我正在使用Node.js v6.5.0,这意味着某些功能可能会丢失.
我对实现什么感到困惑,首先,我的模块将使用Babel,因此实现ES6功能没有问题,其次,我将使用class构造来创建类而不是旧的原型方法.所以现在,我很困惑,如果我要覆盖toString(这是旧的方式)或只是实现Symbol.toStringTag,就像这个MDN文档说的那样,https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag
那么推荐的方法是什么?
我想问一下是否可以添加类似于以下内容的枚举:
STATES = {
WIP: "Work in progress",
ONLINE: "Online",
ONLINE_MODIFIED: "Online, modified",
HIDDEN: "Hidden"
}
Run Code Online (Sandbox Code Playgroud)
在一个类中,并且能够在其他类似于以下内容的文件中使用它:object.updateState(Class.STATES.HIDDEN)而不必构造像这样的新对象boxObject.updateState(new Box().STATES.HIDDEN)
谢谢.
只是尝试使用typescript在类中编写函数.
class Test
{
function add(x: number, y: number): number {
return x + y;
}
}
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误:
TypeScript意外的标记,构造函数,方法,访问器或属性是预期的.
我复制了以下示例:https://www.typescriptlang.org/docs/handbook/functions.html
我错过了什么吗?我糊涂了!
我一直在React js编码.我已经读过在ES6课程中访问'this'我们需要先调用super(道具),我想知道为什么这是.答案我发现主要是谈论Javascript无法知道'这'是什么,除非超类叫做.我想知道这是什么意思,因为在构造函数之外,'this'被识别,我们每次都不调用super(props).
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { /* initial state */ };
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用装饰器(a-la-angular样式)装饰一个类,并向其添加方法和属性。
这是我的装饰类示例:
@decorator
class Person{
}
Run Code Online (Sandbox Code Playgroud)
这是装饰器:
const decorator = (target)=>{
return class New_Class extends target {
myProp:string
}
}
Run Code Online (Sandbox Code Playgroud)
但myProp不是 Person 的已知属性:
person.myProp //Error - myProp does not exist on type Person
Run Code Online (Sandbox Code Playgroud)
如何装饰打字稿类并保留类型完成、类型安全等?
typescript ecmascript-6 es6-class typescript-decorator typescript-class
我在WebSocket使用 es6 类扩展本机类时遇到问题。
以下代码适用于 Chrome 和 Firefox,但不适用于 Safari:
class MyWebSocket extends WebSocket {
doSomething() {
console.log('hi');
}
}
let ws = new MyWebSocket('wss://127.0.0.1:4000');
ws.doSomething();
Run Code Online (Sandbox Code Playgroud)
类型错误:ws.doSomething 不是函数。(在 'ws.doSomething()' 中,'ws.doSomething' 未定义)
console.log('MyWebSocket.prototype')让我看到该功能已添加到原型中。其他一些内置类会发生这种情况,Animation它们是一个,但不会发生在其他类中Date,例如。
有没有人遇到过这个?这是 Safari 中的错误吗?任何建议表示赞赏!
在当前使用ES6类语法和get / set语法的JavaScript项目中,我偶然发现了我无法解释的行为。
首先,一个提取的演示可以按预期工作:
class A {
constructor() {
this.__value = null;
}
get value() {
return this.__value;
}
set value(value) {
this.__value = value;
}
}
class B extends A { }
let b = new B();
b.value = 2;
console.log(b.value); // output: 2
Run Code Online (Sandbox Code Playgroud)
设置和获取b.value(在A.prototype中定义)有效。
现在考虑下面的演示,其中我仅将设置器从A移到了B:
class A {
constructor() {
this.__value = null;
}
get value() {
return this.__value;
}
}
class B extends A {
set value(value) {
this.__value = value;
}
}
let b = new …Run Code Online (Sandbox Code Playgroud) 我正在 vue 项目中编写带有私有属性/方法的 ES6 类。并使用 eslint 对代码进行 lint。下面是示例类:
class TestClass {
constructor(value) {
this.#privateProperty = value
this.#privateMethod(this.#privateProperty)
}
#privateProperty = undefined
// lint error raised at below line
#privateMethod(value) {
this.e = value
console.log(this.e)
}
}
Run Code Online (Sandbox Code Playgroud)
vue 项目由@vue/cli 4.1.2 创建。以下是有关该项目的一些配置:
babel.config.js:
module.exports = {
presets: ['@vue/cli-plugin-babel/preset'],
plugins: [
['@babel/plugin-proposal-class-properties', { loose: true }],
['@babel/plugin-proposal-private-methods', { loose: true }]
]
}
Run Code Online (Sandbox Code Playgroud)
包.json:
{
"name": "demo-project",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"lint": …Run Code Online (Sandbox Code Playgroud) es6-class ×10
javascript ×9
ecmascript-6 ×7
prototype ×2
typescript ×2
class ×1
enums ×1
eslint ×1
getter ×1
node.js ×1
safari ×1