久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Babel 將其替換為 undefined

Babel replaces this with undefined(Babel 將其替換為 undefined)
本文介紹了Babel 將其替換為 undefined的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

這段代碼

beforeEach(() => {
        this.asd= '123';
        this.sdf= '234';
        this.dfg= '345';
        this.fgh= '456';
});

已被 Babel 轉譯為:

has been transpiled by Babel to:

beforeEach(function() {
        undefined.asd= '123';
        undefined.sdf= '234';
        undefined.dfg= '345';
        undefined.fgh= '456';
});

為什么?

推薦答案

推測該代碼在模塊的頂級范圍內,因此處于嚴格模式(模塊默認為嚴格模??式),或者文件正在以嚴格模式進行評估(因為它具有 "use strict" 或因為 Babel 的默認值).

Presumably that code is at the top-level scope of a module, and so it's in strict mode (the default for modules is strict mode), or a file that is being evaluated in strict mode (because it has "use strict" or because of Babel's defaults).

簡短版本:如果您希望 this 在調用回調時由 beforeEach 確定,則您需要使用 function函數,而不是箭頭函數.繼續閱讀為什么 Babel 會按原樣進行轉換:

The short version: If you're expecting this to be determined by beforeEach when calling your callback, you'll want to use a function function, not an arrow function. Read on for why Babel is transpiling as it is:

箭頭函數的基本特性(除了簡潔之外)是它們從上下文繼承this(就像它們關閉的變量一樣),而不是由調用者設置.在嚴格模式下,全局范圍內的 thisundefined.所以 Babel 在編譯時知道箭頭函數中的 this 將是 undefined 并對其進行優化.

The fundamental thing about arrow functions (other than being concise) is that they inherit this from their context (like a variable they close over), instead of having it set by the caller. In strict mode, this at global scope is undefined. So Babel knows, at compile-time, that this within the arrow function will be undefined and optimizes it.

您在評論中說這是在另一個函數中,但我猜它在另一個箭頭函數中,例如:

You've said in comments that this is inside another function, but my guess is that it's inside another arrow function, e.g.:

describe(() => {
    beforeEach(() => {
        this.asd= '123';
        // ...
    });
});

由于 Babel 在 describe 回調中知道 thisundefined,它知道 thisbeforeEach 回調中的 undefined.

Since Babel knows that this is undefined in the describe callback, it also knows that this is undefined in the beforeEach callback.

如果您將代碼置于松散模式上下文中,或在編譯時無法確定 this 的函數調用中,則不會這樣做.例如,在嚴格模式下你的

If you put your code in a loose mode context, or inside a function call where this can't be determined at compile-time, it won't do that. For example, in strict mode your

beforeEach(() => {
  this.asd= '123';
  this.sdf= '234';
  this.dfg= '345';
  this.fgh= '456';
});

確實轉換為

'use strict';
beforeEach(function () {
  undefined.asd = '123';
  undefined.sdf = '234';
  undefined.dfg = '345';
  undefined.fgh = '456';
});

但是這個:

function foo() {
  beforeEach(() => {
    this.asd= '123';
    this.sdf= '234';
    this.dfg= '345';
    this.fgh= '456';
  });
}

轉譯為

'use strict';

function foo() {
  var _this = this;

  beforeEach(function () {
    _this.asd = '123';
    _this.sdf = '234';
    _this.dfg = '345';
    _this.fgh = '456';
  });
}

...因為 Babel 不知道 foo 將如何被調用,因此 this 將是什么.

...because Babel doesn't know how foo will be called, and thus what this will be.

這篇關于Babel 將其替換為 undefined的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 国产中文原创 | 久久久久91 | 国产91一区二区三区 | 久久国产成人 | 国产一区二区三区在线视频 | 亚洲精品免费视频 | 亚洲精品一区二区三区中文字幕 | 欧美1级 | 亚洲h在线观看 | 日本在线综合 | 亚洲国产成人精品女人久久久 | 久久精品一级 | 亚洲精品日韩在线观看 | 久久性色 | 人人做人人澡人人爽欧美 | 久久成人一区二区三区 | 亚洲精品中文字幕在线 | 国产精品亚洲欧美日韩一区在线 | 狠狠插狠狠操 | 国产精品久久久久久久久久 | 成人在线视频免费观看 | 亚洲性人人天天夜夜摸 | 国产精品久久久久久久久久久久久 | 欧美精品一区二区免费视频 | 九九热久久免费视频 | 在线播放国产一区二区三区 | 一区二区手机在线 | 国产在线精品一区二区三区 | 高清视频一区二区三区 | 黄色网一级片 | 精品欧美一区免费观看α√ | 国产伦精品一区二区三区精品视频 | a毛片 | 久久精品成人 | 色视频在线观看 | 欧洲毛片 | 久久99精品国产麻豆婷婷 | 日本高清aⅴ毛片免费 | 日韩av电影在线观看 | 7777在线视频免费播放 | 久久精品色欧美aⅴ一区二区 |