How to override the main function myObject, for example. what does myObject () do without damaging the properties of myObject, e. d. myObject.foo?

I have a legal JS question that is hard to explain. Please forgive my obscure manner of speaking and try to understand this idea .: P

JS objects may have properties. You can assign functions to properties. You can override the properties of an object without changing its other properties.

var myObject = {};
myObject.myFunction = function() { return 1; };
myObject.bar = "Bar";
myObject.myFunction = function() { return "Another function here"; }
console.log( myObject.bar ); // This is still "bar"

But objects themselves can be functions.

Question 1 : What is the correct term for a function of such an object-function? I will call it "main function-function-object" below.

I understand that in JS, all funcitons are functional objects, so it’s incorrect to say "object-object function". But when the object "foo" can perform two functions: foo()and foo.bar(), there must be an unambiguous way to distinguish two in speech.

Because object objects are objects, they can have properties.

Question 2 : How to redefine the main function of a functional object, leaving other properties of the function object unchanged?

An example of what I want:

myFunctionObject = function () { return 1; }
myFunctionObject.bar = "Bar";

// How do i do this...
myFunctionObject = function () { return "Another function here"; }

// ...so that this still returns "Bar"?
console.log( myFunctionObject.bar );

The main function of the function-object must be stored somewhere ... Some hidden property, possibly e. g myFunctionObject.__function__ = function () { return 1; }?

PS - JS. , JS-kungfu [CodeWars] (http://codewars.com.

+3
2

-? " --" .

"". (), , "". -, " " ( "" ).

-, -?

. javascript.

, , , , , .

+1

1. , , , , , , , . .

2: myFunctionObject = function () { return "Another function here"; } myFunctionObject (function () { return "Another function here"; }). , . - , .

function addBar(o){
    o.bar = "Bar";
    return o;
}

myFunctionObject = addBar(function () { return 1; });
myFunctionObject = addBar(function () { return "Another function here"; });
console.log(myFunctionObject.bar);
+1

All Articles