有没有办法在原生脚本的Object.prototype上使用defineProperty?我试图定义方法"IsObject“,但是,当我添加函数时,应用程序崩溃。
该函数为:
Object.defineProperty(Object.prototype, "IsObject", {
value: function ()
{
let obj: Object = this;
return obj !== null && typeof obj === 'object';
}
});发布于 2017-04-27 11:29:25
我刚刚在Android上进行了测试,添加你的代码没有任何问题。我做了几个小改动:
Object.defineProperty(Object.prototype, "isObject", {
value: function ()
{
var obj = this;
return obj !== null && typeof obj === 'object';
}
});
// To Test, I put in a tap handler
var x = {}; console.log("IsObject?:", x.isObject());我降低了isObject的大小写(因为这是项目的正确格式)。我还删除了不需要的TS特定代码;不需要让TS编译器对这个函数做任何事情……
附言:我同意Peter的观点,修改对象原型是很危险的……
https://stackoverflow.com/questions/43627902
复制相似问题