Ask coding questions

← Back to all posts
why?
TheForArkLD (650)

Why i can set key to function?

var func=function(){
return 1;
}
func.key=2
func() //1
func.key //2
Answered by estenoesmiputon (15) [earned 5 cycles]
View Answer
Commentshotnewtop
estenoesmiputon (15)

I think that this is why every variable in JavaScript is an object, so you can assign to an object, properties or attributes like in your example.

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

let car = new Car("here", "some", "stuff")

console.log(car.make, car.model, car.year);

References:
ways-of-create-objects

Hope it helps! Happy codding

Codemonkey51 (793)

:/ I think in js functions can have static vars and that's just setting one like this.key = 2 then func.key would probably return 2 :/ @TheForArkLD