2013-08-24 23 views
124

Tôi đã bắt đầu khám phá Node.js và đã viết nhiều ứng dụng web demo, để hiểu luồng của Node.js, Express.js, ngọc bích, v.v.Câu lệnh 'sử dụng nghiêm ngặt' được diễn giải như thế nào trong Node.js?

Nhưng một điều tôi đã gặp gần đây, là tuyên bố "use strict" làm dòng đầu tiên bên trong mọi chức năng và mọi tệp .js.

Giải thích chính xác Node.js như thế nào?

+2

Chuỗi 'sử dụng nghiêm ngặt'; 'trước bất kỳ câu lệnh nào của hàm sẽ đặt nó ở chế độ nghiêm ngặt (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope /Chế độ nghiêm ngặt). –

+0

Bạn đã đọc sách này chưa? : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode –

+0

điều này không trùng lặp ... Tôi thực sự không biết về JavaScript .. và nếu nó có trong JavaScript. một số khác biệt sẽ ở đó .. đúng không? Vì vậy, hãy cung cấp một số hiểu biết về node.js –

Trả lời

139

"use strict";

Về cơ bản nó cho phép chế độ nghiêm ngặt.

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.

Theo nhận xét của bạn, bạn sẽ nói một số khác biệt sẽ có ở đó. Nhưng đó là giả định của bạn. Mã Node.js không là gì ngoài mã JavaScript của bạn. Tất cả mã Node.js được diễn giải bởi V8 JavaScript engine. Động cơ JavaScript V8 là một công cụ JavaScript mã nguồn mở do Google phát triển cho trình duyệt web Chrome.

Vì vậy, sẽ không có sự khác biệt lớn về cách trình duyệt Chrome và Node.js.

Vui lòng đọc chế độ nghiêm ngặt trong JavaScript là gì.

Để biết thêm thông tin:

  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 Mã & chế độ nghiêm ngặt. Sau đây là tóm tắt 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.

Ngoài ra nếu bạn đang bị mất trên những tính năng được hỗ trợ bởi phiên bản hiện tại của bạn Node.js, node.green này có thể giúp bạn (thúc đẩy từ các dữ liệu giống như kangax).

+8

Tại sao câu trả lời này được giảm xuống mà không có bất kỳ nhận xét nào? –

+7

Trước hết, Chrome không phải là trình duyệt duy nhất trên hành tinh này. Chỉ cần Node.js và Chrome sử dụng V8, không có nghĩa là nó hoạt động giống nhau trong tất cả các trình duyệt. Thứ hai, có một sự khác biệt trong cách, các tập tin (aka module) được nạp. Thứ ba, các kịch bản cho trình duyệt thường được ghép nối để sử dụng sản xuất và đó là nơi các vấn đề có thể phát sinh khi bạn chỉ nói rằng cả hai đều là cùng một môi trường. Họ không phải. V8 không phải là điều quan trọng duy nhất khi nói đến việc thực thi các tệp Node.js. –

+8

@GabrielLlamas: Cảm ơn. @ Golo Roden: Có, nếu nhận xét đúng. chúng tôi có thể đưa ra câu trả lời tốt hơn. –

Các vấn đề liên quan