文件第一行的 "use strict" 是否会影响文件中的所有功能?
Does "use strict" in the first line of a file affect all the functions in the file?
所以为了学习目的,我从 here 改编了这段代码:
"use strict";
/// <reference path="./definitelyTyped/node/node.d.ts" />
import * as net from "net";
let sockets: net.Socket[] = [];
/*
* Cleans the input of carriage return, newline
*/
function cleanInput(data: Buffer): string {
return data.toString().replace(/(\r\n|\n|\r)/gm, "");
}
/*
* Method executed when data is received from a socket
*/
function receiveData(socket: net.Socket, data: Buffer): void {
let cleanData: string = cleanInput(data);
if (cleanData === "@quit") {
socket.end("Goodbye!\r\n");
} else {
for (var i: number = 0; i < sockets.length; i++) {
if (sockets[i] !== socket) {
sockets[i].write(data);
}
}
}
}
/*
* Method executed when a socket ends
*/
function closeSocket(socket: net.Socket): void {
let i: number = sockets.indexOf(socket);
if (i !== -1) {
sockets.splice(i, 1);
}
}
/*
* Callback method executed when a new TCP socket is opened.
*/
function newSocket(socket: net.Socket): void {
sockets.push(socket);
socket.write("Welcome to the Telnet server!\r\n");
socket.on("data", function(data: Buffer): void {
receiveData(socket, data);
});
socket.on("end", function(): void {
closeSocket(socket);
});
}
// create a new server and provide a callback for when a connection occurs
let server: net.Server = net.createServer(newSocket);
// listen on port 4444
server.listen(4444);
console.log("started");
我是运行tslint on this file with these tslint.json规则。
这是我得到的:
app.ts[10, 1]: missing 'use strict'
app.ts[17, 1]: missing 'use strict'
app.ts[33, 1]: missing 'use strict'
app.ts[43, 1]: missing 'use strict'
我的理解是,文件第一行的 "use string";
在整个文件中应该是全局的。然而,每个函数都会产生一条消息。我错过了什么吗?
可以在全局范围或单个函数内启用严格模式:
// Enable strict mode globally
"use strict";
var v = "This script is in strict mode.";
或本地:
function strict() {
'use strict';
function nested() {
return "Nested function is also in strict mode.";
}
return "Function is in strict mode. " + nested();
}
function notStrict() {
return "This function is not in strict mode.";
}
My understanding, is that "use string"; on the first line of the file should be global across the whole file. Yet, each of the functions yields a message
您的理解是正确的。但是,您请求 tslint 确保它适用于所有 函数 以及 "check-function"
set to true。将其设置为 false,因为如果您已启用模块级别则不需要它。
所以为了学习目的,我从 here 改编了这段代码:
"use strict";
/// <reference path="./definitelyTyped/node/node.d.ts" />
import * as net from "net";
let sockets: net.Socket[] = [];
/*
* Cleans the input of carriage return, newline
*/
function cleanInput(data: Buffer): string {
return data.toString().replace(/(\r\n|\n|\r)/gm, "");
}
/*
* Method executed when data is received from a socket
*/
function receiveData(socket: net.Socket, data: Buffer): void {
let cleanData: string = cleanInput(data);
if (cleanData === "@quit") {
socket.end("Goodbye!\r\n");
} else {
for (var i: number = 0; i < sockets.length; i++) {
if (sockets[i] !== socket) {
sockets[i].write(data);
}
}
}
}
/*
* Method executed when a socket ends
*/
function closeSocket(socket: net.Socket): void {
let i: number = sockets.indexOf(socket);
if (i !== -1) {
sockets.splice(i, 1);
}
}
/*
* Callback method executed when a new TCP socket is opened.
*/
function newSocket(socket: net.Socket): void {
sockets.push(socket);
socket.write("Welcome to the Telnet server!\r\n");
socket.on("data", function(data: Buffer): void {
receiveData(socket, data);
});
socket.on("end", function(): void {
closeSocket(socket);
});
}
// create a new server and provide a callback for when a connection occurs
let server: net.Server = net.createServer(newSocket);
// listen on port 4444
server.listen(4444);
console.log("started");
我是运行tslint on this file with these tslint.json规则。
这是我得到的:
app.ts[10, 1]: missing 'use strict'
app.ts[17, 1]: missing 'use strict'
app.ts[33, 1]: missing 'use strict'
app.ts[43, 1]: missing 'use strict'
我的理解是,文件第一行的 "use string";
在整个文件中应该是全局的。然而,每个函数都会产生一条消息。我错过了什么吗?
可以在全局范围或单个函数内启用严格模式:
// Enable strict mode globally
"use strict";
var v = "This script is in strict mode.";
或本地:
function strict() {
'use strict';
function nested() {
return "Nested function is also in strict mode.";
}
return "Function is in strict mode. " + nested();
}
function notStrict() {
return "This function is not in strict mode.";
}
My understanding, is that "use string"; on the first line of the file should be global across the whole file. Yet, each of the functions yields a message
您的理解是正确的。但是,您请求 tslint 确保它适用于所有 函数 以及 "check-function"
set to true。将其设置为 false,因为如果您已启用模块级别则不需要它。