我使用的是TypeScript版本1.0.1.0
当同时使用TypeScript和AMD模块时,我应该在哪里写下“use strict”语句?它应该高于还是低于进口?
我可以在导入上面编写它,以便这个TypeScript代码:
"use strict";
import Backbone = require('backbone');
class MyClass extends Backbone.View<Backbone.Model> { }
export = MyClass;
会在此JavaScript中生成语句顶部的语句:
"use strict";
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
define(["require", "exports", 'backbone'], function(require, exports, Backbone) {
var MyClass = (function (_super) {
__extends(MyClass, _super);
function MyClass() { _super.apply(this, arguments); }
return MyClass;
})(Backbone.View);
return MyClass;
});
或者我可以在我的导入下面加上“use strict”语句。所以这个TypeScript:
import Backbone = require('backbone');
"use strict";
class MyClass extends Backbone.View<Backbone.Model> { }
export = MyClass;
使用RequireJS函数声明顶部的语句生成此Javascript:
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
define(["require", "exports", 'backbone'], function(require, exports, Backbone) {
"use strict";
var MyClass = (function (_super) {
__extends(MyClass, _super);
function MyClass() { _super.apply(this, arguments); }
return MyClass;
})(Backbone.View);
return MyClass;
});
哪一个是正确的?即使语句完全丢失,TSLint也不会报告违规行为。这可能是TSLint中的一个错误吗?
答案 0 :(得分:4)
第一个变体导致整个脚本严格:
"use strict";
import Backbone = require('backbone');
class MyClass extends Backbone.View<Backbone.Model> { }
export = MyClass;
第二个将严格范围限制为(生成)函数:
import Backbone = require('backbone');
"use strict";
class MyClass extends Backbone.View<Backbone.Model> { }
export = MyClass;
在实践中,它在你的情况下没什么区别,因为它只是严格声明之间的区别,包括自动生成&#34;扩展&#34;代码与否 - 自动生成的代码严格遵守。