2013-08-24 20 views
124

Node.js'yi keşfetmeye başladım ve Node.js, Express.js, yeşim, vb. Akışını anlamak için birçok demo web uygulaması yazdım.Node.js'de 'use strict' ifadesi nasıl yorumlanır?

Son zamanlarda karşılaştığım bir şey var. "use strict" ifadesini, her işlev içindeki ilk satır ve her .js dosyasında bulabilirsiniz.

Node.js tarafından tam olarak nasıl yorumlanır?

+2

A ''use strict';' string herhangi bir fonksiyonun ifadesinden önce [katı mod] 'a girer (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode). –

+0

Bunu okudun mu? : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode –

+0

bu yinelenmez ... JavaScript'i gerçekten bilmiyorum .. ve eğer JavaScript’te varsa. bazı farklılıklar olacak .. doğru mu? Bu yüzden lütfen node.js hakkında biraz bilgi veriniz. –

cevap

139

"use strict";

Temel olarak sıkı modu sağlar.

Strict Mode is a feature that allows you to place a program, or a function, in a "strict" operating context. In strict operating context, the method form binds this to the objects as before. The function form binds this to undefined, not the global set objects.

Yorumlarınızda bazı farklılıkların var olacağını söylüyorsunuz. Ama bu senin varsayımın. Node.js kodu, JavaScript kodunuzdan başka bir şey değildir. Tüm Node.js kodları V8 JavaScript engine tarafından yorumlanır. V8 JavaScript Engine, Google for Chrome web tarayıcısı tarafından geliştirilen bir açık kaynak kodlu JavaScript motorudur.

"use strict";'un Chrome tarayıcı ve Node.js. tarafından nasıl yorumlandığı önemli bir fark olmayacaktır.

Lütfen JavaScript'deki sıkı modun ne olduğunu okuyun.

  1. Strict mode
  2. ECMAScript 5 Strict mode support in browsers
  3. Strict mode is coming to town
  4. Compatibility table for strict mode
  5. Stack Overflow questions: what does 'use strict' do in JavaScript & what is the reasoning behind it

: Daha fazla bilgi için

ECMAScript 6:

ECMAScript 6 Kod & düzey modu.

Eğer node.js tutarındaki mevcut sürümü tarafından desteklenen hangi özellikleri üzerinde kaybolur Ayrıca eğer

10.2.1 Strict Mode Code

An ECMAScript Script syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. Code is interpreted as strict mode code in the following situations:

  • Global code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1.1).
  • Module code is always strict mode code.
  • All parts of a ClassDeclaration or a ClassExpression are strict mode code.
  • Eval code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive or if the call to eval is a direct eval (see 12.3.4.1) that is contained in strict mode code.
  • Function code is strict mode code if the associated FunctionDeclaration, FunctionExpression, GeneratorDeclaration, GeneratorExpression, MethodDefinition, or ArrowFunction is contained in strict mode code or if the code that produces the value of the function’s [[ECMAScriptCode]] internal slot begins with a Directive Prologue that contains a Use Strict Directive.
  • Function code that is supplied as the arguments to the built-in Function and Generator constructors is strict mode code if the last argument is a String that when processed is a FunctionBody that begins with a Directive Prologue that contains a Use Strict Directive.

, bu node.green size yardımcı olabilir (kangax aynı verilerden yararlanır): Aşağıdaki kısa from the specification olduğunu.

+8

Bu cevap neden herhangi bir yorum yapmadan reddedildi? –

+7

Her şeyden önce, Chrome gezegendeki tek tarayıcı değil. Sadece bu Node.js ve Chrome V8 kullanıyor, tüm tarayıcılarda aynı şekilde çalıştığı anlamına gelmez. İkincisi, yolda bir fark var, dosyalar (aka modülleri) yüklenir. Üçüncü olarak, tarayıcılar için komut dosyaları genellikle üretim kullanımı için birleştirilir ve bu, her ikisinin de aynı ortamlar olduğunu söylerken sorunların ortaya çıkabileceği yerdir. Onlar değil. Node.js dosyalarını yürütmek için V8 tek önemli şey değildir. –

+8

@GabrielLlamas: Teşekkürler. @ Golo Roden: Evet, düzgün yorumlandıysa. daha iyi cevaplar verebiliriz. –

İlgili konular