在编译后将Typescript基本赋值到if语句中

时间:2016-02-28 07:42:55

标签: javascript typescript angular

我尝试从Angular2 Quickstart开始,在app.component.js(版本1.8.2)编译tsc之后app.component.ts内发现错误(功能?):

if (d = decorators[i])

我没有在快速入门示例配置中找到错误的地方。 谢谢你的帮助!

我的代码:

app.component.ts

import {Component} from 'angular2/core';


@Component({
    selector: 'my-app',
    templateUrl: '/templates/AppComponent.html'
})
export class AppComponent {
    public name: string = "My test app";
}

的package.json

{
  "name": "iSandBox",
  "version": "1.0.0",
  "scripts": {
    "start": "concurrent \"npm run tsc:w\" \"npm run grunt\" \"npm run lite\" ",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install",
    "grunt": "grunt"
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.7",
    "systemjs": "0.19.22",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.34.4",
    "reflect-metadata": "0.1.3",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.5.15"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "grunt": "^1.0.0-rc1",
    "grunt-contrib-copy": "^0.8.2",
    "grunt-contrib-jshint": "~1.0.0",
    "grunt-contrib-nodeunit": "~0.4.1",
    "grunt-contrib-uglify": "~0.11.1",
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.0",
    "gulp-rename": "^1.2.2",
    "gulp-tslint": "^4.3.2",
    "gulp-uglify": "^1.5.3",
    "gulp-util": "^3.0.7",
    "lite-server": "^2.1.0",
    "typescript": "^1.7.5",
    "typings": "^0.6.8"
  },
  "ambientDependencies": {
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

app.component.js

System.register([ 'angular2/core' ],
    function(exports_1, context_1) {
        "use strict";
        var __moduleName = context_1 && context_1.id;
        var __decorate = (this && this.__decorate)
                || function(decorators, target, key, desc) {
                    var c = arguments.length, r = c < 3 ? target
                            : desc === null ? desc = Object
                                    .getOwnPropertyDescriptor(target, key) : desc, d;
                    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
                        r = Reflect.decorate(decorators, target, key, desc);
                    else
                        for (var i = decorators.length - 1; i >= 0; i--)
/*=================HERE===>>>>*/  if (d = decorators[i])
                                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
                    return
                            c > 3 && r && Object.defineProperty(target, key, r), r;
                };
        var __metadata = (this && this.__metadata)
                || function(k, v) {
                    if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
                        return Reflect.metadata(k, v);
                };
        var core_1;
        var AppComponent;
        return {
            setters : [ function(core_1_1) { core_1 = core_1_1; } ],
            execute : function() {
                AppComponent = (function() {
                    function AppComponent() {
                        this.name = "My test app";
                    }
                    AppComponent = __decorate(
                            [core_1.Component({
                                        selector : 'my-app',
                                        templateUrl : '/templates/AppComponent.html'
                                    }),
                            __metadata('design:paramtypes',[]) ], AppComponent);
                    return AppComponent;
                }());
                exports_1("AppComponent", AppComponent);
            }
        }
    });
// # sourceMappingURL=app.component.js.map

1 个答案:

答案 0 :(得分:3)

测试中的分配不一定是错误的。这样:

if (d = decorators[i])

与此相同:

d = decorators[i];
if (d)

让我们看看整个if

if (d = decorators[i])
    r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;

正如你所看到的那样,if的主体正在使用d作为一个函数,所以这只是一个警卫,以防止调用一些错误的东西(因此不是一个功能)。

它只是循环遍历decorators,而对于非虚假的,则会调用它们。