如何确定函数是一种方法的对象?

时间:2022-12-06 07:31:20
var A = {
    x : function () { }
};

var b = function (method) {
    //want to know method's "parent" here
};

b(A.x);

I want to know that x is defined in A when I call the b(A.x). Is this possible?

我想知道当我调用b(A.x)时,x在A中定义。这可能吗?

4 个解决方案

#1


4  

There's no nice built-in way to do this, because actually there are no methods in Javascript. They are independent Function objects that just happen to be assigned somewhere.

没有很好的内置方法来实现这一点,因为实际上Javascript中没有方法。它们是恰好恰好在某处分配的独立Function对象。

If you create new instance of function every time (e.g. closure) [thanks Matthew Crumley for pointing that out], then you can modify the function object to explicitly associate it with its parent:

如果你每次都创建新的函数实例(例如关闭)[感谢Matthew Crumley指出这一点],那么你可以修改函数对象以显式地将它与其父对象关联:

x.parent = A;

and then you can call it properly as if it was a method:

然后你可以正确地调用它,就像它是一个方法:

x.call(x.parent);

Otherwise you will have to pass both function and its parent object.

否则,您必须传递函数及其父对象。

#2


2  

This question makes little sense from the perspective of the language as a function may exist on many objects.

从语言的角度来看这个问题没什么意义,因为许多对象上可能存在一个函数。

var a = { name : 'a' },
    b = { name : 'b' },
    c = { name : 'c' }; 
a.x = function () { alert( this.name ); };
c.x = b.x = a.x;  // a, b, and c all reference the same function

You may call the x function with any context you want:

您可以使用您想要的任何上下文调用x函数:

a.x(); // alerts "a" because "this" is object a 
b.x(); // alerts "b" because "this" is object b 
a.x.call( b ); // alerts "b" because "this" is object b (via parameter)

You can manipulate this behavior to work for you:

您可以操纵此行为以适合您:

var b = function ( method ) {
  // parent = this;
};
b.call( A, A.x );

There isn't however any way of knowing from inside a function what object it is assigned to as this isn't necessarily a single place.

然而,没有任何方法可以从函数内部知道它被分配给哪个对象,因为这不一定是单个位置。

#3


2  

Even adding a parent property won't work in all cases, because if the function is in the object's prototype, there is only one copy of the function object, so there's no way to tell which instance it came from. Here's an example to show the problem:

即使添加父属性也不会在所有情况下都起作用,因为如果函数在对象的原型中,则只有一个函数对象的副本,因此无法分辨它来自哪个实例。这是一个显示问题的示例:

function MyClass() {
    // Create a MyClass object
}
MyClass.prototype.x = function() { return 42; };

var a = new MyClass();
a.x.parent = a; // Set the parent to a

var b = new MyClass();
b.x.parent = b; // b.x and a.x both reference the same function from MyClass.prototype

Now, a.x.parent and b.x.parent are both set to b.

现在,a.x.parent和b.x.parent都设置为b。

@porneL's method will work as long as each object gets its own copy of the function.

只要每个对象都获得自己的函数副本,@ porneL的方法就会起作用。

It's probably better to modify the function to take a parent object and a method so it will work with any function.

修改函数以获取父对象和方法可能更好,因此它可以与任何函数一起使用。

#4


0  

Every function in JavaScript is actually a Function object.

JavaScript中的每个函数实际上都是一个Function对象。

<html>
<body>
<script>
var A = {
    x: function (a_a, a_b) { alert(a_a + a_b);  }
};

var b = function (a_method) {
    alert(a_method.toString());
    a_method.call(this, 1, 2);
};

b(A.x);
</script>

#1


4  

There's no nice built-in way to do this, because actually there are no methods in Javascript. They are independent Function objects that just happen to be assigned somewhere.

没有很好的内置方法来实现这一点,因为实际上Javascript中没有方法。它们是恰好恰好在某处分配的独立Function对象。

If you create new instance of function every time (e.g. closure) [thanks Matthew Crumley for pointing that out], then you can modify the function object to explicitly associate it with its parent:

如果你每次都创建新的函数实例(例如关闭)[感谢Matthew Crumley指出这一点],那么你可以修改函数对象以显式地将它与其父对象关联:

x.parent = A;

and then you can call it properly as if it was a method:

然后你可以正确地调用它,就像它是一个方法:

x.call(x.parent);

Otherwise you will have to pass both function and its parent object.

否则,您必须传递函数及其父对象。

#2


2  

This question makes little sense from the perspective of the language as a function may exist on many objects.

从语言的角度来看这个问题没什么意义,因为许多对象上可能存在一个函数。

var a = { name : 'a' },
    b = { name : 'b' },
    c = { name : 'c' }; 
a.x = function () { alert( this.name ); };
c.x = b.x = a.x;  // a, b, and c all reference the same function

You may call the x function with any context you want:

您可以使用您想要的任何上下文调用x函数:

a.x(); // alerts "a" because "this" is object a 
b.x(); // alerts "b" because "this" is object b 
a.x.call( b ); // alerts "b" because "this" is object b (via parameter)

You can manipulate this behavior to work for you:

您可以操纵此行为以适合您:

var b = function ( method ) {
  // parent = this;
};
b.call( A, A.x );

There isn't however any way of knowing from inside a function what object it is assigned to as this isn't necessarily a single place.

然而,没有任何方法可以从函数内部知道它被分配给哪个对象,因为这不一定是单个位置。

#3


2  

Even adding a parent property won't work in all cases, because if the function is in the object's prototype, there is only one copy of the function object, so there's no way to tell which instance it came from. Here's an example to show the problem:

即使添加父属性也不会在所有情况下都起作用,因为如果函数在对象的原型中,则只有一个函数对象的副本,因此无法分辨它来自哪个实例。这是一个显示问题的示例:

function MyClass() {
    // Create a MyClass object
}
MyClass.prototype.x = function() { return 42; };

var a = new MyClass();
a.x.parent = a; // Set the parent to a

var b = new MyClass();
b.x.parent = b; // b.x and a.x both reference the same function from MyClass.prototype

Now, a.x.parent and b.x.parent are both set to b.

现在,a.x.parent和b.x.parent都设置为b。

@porneL's method will work as long as each object gets its own copy of the function.

只要每个对象都获得自己的函数副本,@ porneL的方法就会起作用。

It's probably better to modify the function to take a parent object and a method so it will work with any function.

修改函数以获取父对象和方法可能更好,因此它可以与任何函数一起使用。

#4


0  

Every function in JavaScript is actually a Function object.

JavaScript中的每个函数实际上都是一个Function对象。

<html>
<body>
<script>
var A = {
    x: function (a_a, a_b) { alert(a_a + a_b);  }
};

var b = function (a_method) {
    alert(a_method.toString());
    a_method.call(this, 1, 2);
};

b(A.x);
</script>