var Assertion = function() {
return { "dummy": "data" };
}
Object.defineProperty(Object.prototype, 'should', {
set: function(){},
get: function(){
return new Assertion(this);
}
});
// Insert magic here.
// This needs to be false
console.log(({}).should === undefined);
Run Code Online (Sandbox Code Playgroud)
我在ES5中有哪些选项来撤消defineProperty呼叫?
没有愚蠢的建议,Object.defineProperty = function() { }请.
下列 Object.defineProperty(Object.prototype, 'should', {})
和 Object.defineProperty(Object.prototype, 'should', { value: undefined })
Uncaught TypeError: Cannot redefine property: defineProperty在V8中投掷a
Object.defineProperty(Object.prototype, 'should', {
set: function() {},
get: function() { return undefined; }
}); …Run Code Online (Sandbox Code Playgroud) 我知道这是不受欢迎的,我只是在探索这个想法,因为我的生活似乎无法按照我想要的方式来完成这项工作.
这个例子应该解释所有:
String.prototype.MyNS = function() {}
String.prototype.MyNS.fooify = function() {
return this + 'foo!';
}
var theString = 'Kung';
alert(theString.MyNS.fooify());
Run Code Online (Sandbox Code Playgroud)
当然,这只是将函数定义附加到'foo'...添加this()而不起作用.
我知道我在那里失去了背景,但无法弄清楚如何使原件发射并给我我想要的东西.
我正在尝试做这样的事情。
var myFunc = function() {}
myFunc.prototype = new String();
myFunc.prototype.replace = function() {return 'hii, Mr '+ this.toString();}
var oVal = new myFunc('Jyotirmay');
oVal.replace();
Run Code Online (Sandbox Code Playgroud)
o / p ::未捕获的TypeError:String.prototype.toString不是通用的(...)
为什么通常会出现“非通用功能”错误?
更清楚地说,我如何将我的参数(即Jyotirmay)从继承的类传递到基类(即字符串)。这样我就可以通过调用任何适当的字符串函数来获取该值。
我不想通过处理函数中的变量来获取传递的值。我希望由父类处理。您可以用其他语言说出super()。