具有继承属性的函数(可调用)对象
Function (callable) objects with inherited properties
是否有办法创建一个函数/可调用对象,从另一个对象继承属性?这对于__proto__
是可能的,但该属性已弃用/非标准。是否有一种符合标准的方法来做到这一点?
/* A constructor for the object that will host the inheritable properties */
var CallablePrototype = function () {};
CallablePrototype.prototype = Function.prototype;
var callablePrototype = new CallablePrototype;
callablePrototype.hello = function () {
console.log("hello world");
};
/* Our callable "object" */
var callableObject = function () {
console.log("object called");
};
callableObject.__proto__ = callablePrototype;
callableObject(); // "object called"
callableObject.hello(); // "hello world"
callableObject.hasOwnProperty("hello") // false
这在标准方式下似乎是不可能的。
你确定不能直接复制吗?
function hello(){
console.log("Hello, I am ", this.x);
}
id = 0;
function make_f(){
function f(){
console.log("Object called");
}
f.x = id++;
f.hello = hello;
return f;
}
f = make_f(17);
f();
f.hello();
g = make_f(17);
g();
g.hello();
(如果我必须这样做,我也会隐藏id
, hello
和类似的东西在闭包内,而不是使用全局)
相关文章:
- JQuery Deferred Ajax,将结果保存在调用对象中
- 无法从JavaScript中的函数调用对象属性
- 在Javascript中调用对象方法时不是函数类型错误
- 正在调用对象中设置的匿名函数,但未得到预期结果
- 为什么需要“this”来调用对象内部的函数
- 如何从onClick事件调用对象函数
- 是否可以在不加对象名称前缀的情况下调用对象的函数
- 对象-在定义对象之前先调用对象
- console.log表示,当我调用对象的任何成员时,对象都是未定义的
- 正在调用对象的方法
- console.log是否调用对象的toString方法
- Javascript”;无效的调用对象“;xml错误
- 如何在javascript中调用对象键(codecademy)
- 如果我丢失了调用对象,如何杀死 setInterval()/setTimout()
- 如何在对象生成器外部调用对象属性值
- JavaScript:动态调用对象内的函数
- 如何动态加载 JavaScript 对象或调用对象方法
- IE9 在 iframe 中打印 PDF 时出现“无效调用对象”
- 在方法调用对象之外访问变量而不使它们全局化(可能吗?
- Javascript - 在对象中调用对象的方法,给出错误:“对象没有方法getStartPoint”