[javascript] Is the object a function?

Asked 2 years ago, Updated 2 years ago, 122 views

Hi, everyone.

var color = 'red';
function foo() {
    var color = 'blue'; // 2
    function bar() {
        console.log(color); // 1
    }
    return bar;
}

When you want to get the color variable of the foo function from the following code

function foo() { ... }
foo.color = "blue";
console.log(foo.color); // "blue"

I found out that I can import foo.color into .

Question 1) Is the foo of function foo() above a function? Is it an object?

Also, I've created another code to print foo.color. When I wrote the code below, undefined came out. I haven't... I didn't study the basics well. Object varfoo = {color:'blue'}That's all I've said.

Question 2) Is the method below wrong? Why is undefined?

function foo() {
     foo.color = 'blue'; 
}
console.log(foo.color);

And... I looked for stackoverflow because I was curious about the relationship between the object and the function. https://stackoverflow.com/questions/3449596/every-object-is-a-function-and-every-function-is-object-which-is-correct

Looking at the answers in the link below,

Since you can't "call" every Object instance, not every object is a function.

So it says that a function is all objects, but an object cannot be said to be a function.

Question 3) What does the call here mean?

I don't know if it's the 'call method' of the Function.prototype.call() function, or 'call' like var foo = new Foo();

The writing is a little long, and there are several questions. Thank you for reading :) Answer is always very helpful to meㅜ<

javascript object function

2022-09-21 22:05

2 Answers

var color and foo.color in the function foo are completely different variables.
It doesn't mean that var color can be obtained as foo.color, but foo.color can be used to express foo's color.
The regional variable var color still has no choice but to return.

Answer 1)
As shown in the stack overflow you uploaded, all functions are objects.
Therefore, foo is also an object, especially a function.

Answer 2)
Try running the code one by one.
The code in the foo function is executed only when foo is called.
This means that foo must be invoked to run foo.color='blue'; to set foo.color.

function foo() {
     foo.color = 'blue'; 
}
foo();
console.log(foo.color);

This will work, but you won't have to write it like this because these codes come from lack of understanding of JS in the first place.

Answer 3)
Both of them.
Foo.call(), newFoo(), foo()All calls (call).
So every function is an object, and what you can't call is an object, but it's not a function.


2022-09-21 22:05

I'll add a little bit.

javascript is a first-class object language.

You can easily factor a function and receive it as a return value.

This is called a higher order function, and javascript supports it.

Among the objects in javascript, the callable object is a function. This means that not all objects in JavaScript can be called.

And the problem of variables in a function requires understanding the phenomenon of hoisting. In particular, it is required to understand the code in NeuroWhAl's answer 2.

First of all, there are var, let, and const for es6 criteria. Among them, var and let must be understood and used well.

Let behaves like a local variable in other languages (not hosting), but var does not. It is easily raised to a global variable and declared.

console.log(v);
var v = 'test';
console.log(v);

undefined
test

In fact, what we want is not undefined, but an error like vis not defined. Because undefined means that there are variables.

The code above is actually executed by pulling up the variable v as shown below.

var v;
console.log(v);
v = 'test';
console.log(v);

You should now use let, const rather than var, and review the 'use strict' declaration when using var.


2022-09-21 22:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.