Javascript 使用Typescript的属性名冲突

Javascript 使用Typescript的属性名冲突,javascript,typescript,Javascript,Typescript,我遇到了一个我认为是与函数对象()上的保留属性冲突的问题,我想知道是否有一种方法可以在我下面的“home”类中使用名为“name”的静态变量,所有变量都是小写的,而不会与Function.name冲突 原始javascript: // Note: I've changed the "home" class to "_c0" here to show the problem more clearly. var myApi; (function (myApi) { var _c0 = (fu

我遇到了一个我认为是与函数对象()上的保留属性冲突的问题,我想知道是否有一种方法可以在我下面的“home”类中使用名为“name”的静态变量,所有变量都是小写的,而不会与Function.name冲突

原始javascript:

// Note: I've changed the "home" class to "_c0" here to show the problem more clearly.
var myApi;
(function (myApi) {
    var _c0 = (function () {
        function _c0() { }
        _c0.name = "Home"; // this is colliding with the "Function.name". there is no "name" property on this object during runtime
        _c0.Name = "Home";
        _c0.name1 = "Home";
        _c0.url = "/Home";
        return _c0;
    })();
    myApi.home = _c0;
})(myApi || (myApi = {}));

console.log(myApi.home.name); // prints "_c0" <- this is the name of the function
console.log(myApi.home.Name); // prints "Home"
console.log(myApi.home.name1); // prints "Home"
console.log(myApi.home.url); // prints "/Home"

for(var prop in myApi.home)
    console.log(prop + ": " + myApi.home[prop]);
/* prints: (note the lack of "name: Home" in the output)
    Name: Home
    name1: Home
    url: /Home
*/
module myApi {
    export class home {
        static name = "Home";
        static Name = "Home"; // just testing
        static name1 = "Home"; // just testing
        static url = "/Home";
    }
}

console.log(myApi.home.name);
console.log(myApi.home.Name);
console.log(myApi.home.name1);
console.log(myApi.home.url);
for (var prop in myApi.home)
    console.log(prop + ": " + myApi.home[prop]);

不,没有办法做到这一点。函数对象具有
名称
属性,不能重写。下面的代码片段中可以找到这方面的简单证明:

var F = function(){}; 
Object.defineProperty(F, "name", {value: "Home"}); 
//TypeError: Cannot redefine property: name
这意味着静态
name
属性是只读的。无法输入新值,因为该值的描述符会阻止输入。我建议你为你的财产选择另一个名字