2013-08-24 9 views
124

Zacząłem eksplorować Node.js i napisałem wiele aplikacji internetowych do demo, aby zrozumieć przepływ Node.js, Express.js, jade, itp.W jaki sposób interpretacja "użyj ścisłego" jest interpretowana w pliku Node.js?

Ale jedno, co ostatnio spotkałem, to oświadczenie "use strict" jako pierwsza linia wewnątrz każdej funkcji i każdego pliku .js.

Jak dokładnie jest interpretowany przez Node.js?

+2

Łańcuch '' use strict '; 'przed dowolnym wyrażeniem funkcji umieści go w [trybie ścisłym] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope /Tryb ścisły). –

+0

Czytałeś to? : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode –

+0

to nie jest duplikat ... Naprawdę nie wiem o JavaScript .. i czy jest tam w JavaScript. niektóre różnice będą tam .. prawda? więc proszę podać kilka informacji na temat node.js –

Odpowiedz

139

"use strict";

Zasadniczo umożliwia tryb ścisły.

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.

Zgodnie z Twoimi komentarzami mówisz, że będą tam pewne różnice. Ale to twoje założenie. Kod Node.js to nic innego jak twój kod JavaScript. Cały kod Node.js jest interpretowany przez V8 JavaScript engine. Silnik V8 JavaScript Engine to silnik JavaScript o otwartym kodzie źródłowym opracowany przez Google dla przeglądarki Chrome.

Tak więc nie będzie dużej różnicy w interpretacji "use strict"; przez przeglądarkę Chrome i Node.js.

Proszę przeczytać, co jest trybem ścisłym w JavaScript.

Aby uzyskać więcej informacji:

  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


ECMAScript 6:

ECMAScript 6 Kod & tryb ścisły. Poniżej krótki from the specification:

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.

Dodatkowo jeśli jesteś zgubiony na jakie funkcje są obsługiwane przez aktualnej wersji node.js, to node.green może pomóc (korzysta z tych samych danych jak kangax).

+8

. Dlaczego ta odpowiedź jest bez komentarza? –

+7

Po pierwsze, Chrome nie jest jedyną przeglądarką na świecie. Tylko że Node.js i Chrome używają V8, nie oznacza to, że działa on tak samo we wszystkich przeglądarkach. Po drugie, istnieje różnica w sposobie ładowania plików (takich jak moduły). Po trzecie, skrypty dla przeglądarek są zwykle łączone ze sobą do użytku produkcyjnego, i tam mogą pojawić się problemy, gdy po prostu mówisz, że oba są tymi samymi środowiskami. Oni nie są. V8 nie jest jedyną ważną rzeczą, jeśli chodzi o wykonywanie plików Node.js. –

+8

@GabrielLlamas: Dzięki. @ Golo Roden: Tak, jeśli komentarz został poprawnie skomentowany. możemy dać lepsze odpowiedzi. –

Powiązane problemy