I ran into a what I think is a collision with a reserved property on the Function object ( Function.name ) and I'm wondering if there is a way to have a static variable called "name" on my "home" class below, all in lowercase, without conflicting with Function.name?
raw 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
*/
Typescript file:
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]);
No there isn't a way to do that. Function objects have the name
property and it cannot be overridden. Simple proof of that can be found in the snippet below:
var F = function(){};
Object.defineProperty(F, "name", {value: "Home"});
//TypeError: Cannot redefine property: name
What this means is that the static name
property is readonly. A new value cannot be entered, as it is prevented by the descriptor of that value. I suggest you choose another name for your property.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.