I study JS, and noticed that after creating an object, an external function can be made its method, while a new element inside the object (for storing the function) is created right during assignment. If we want to add a function to the constructor, we can only assign an external function to an element that already exists in the constructor.

The question is - is there a way to create a new element in the constructor after its creation (and assign it a value, for example, a function)?

Here is an example. If I understand correctly, dog.newSound = changeSound; works because it is an object in animal.newSound = changeSound; It does not work because it is a constructor. Is there any other way to do the same for the constructor?

 //Конструктор function Animal(name, age, sound) { this.name = name; this.age = age; this.sound = sound; } //внешняя функция function changeSound(sound) { this.sound = sound; } //объект созданный с помощью конструктора var dog = new Animal("Jack", 5, "gav-gav") //создали переменную в объекте и присвоили ей функцию dog.newSound = changeSound; dog.newSound("woof-woof"); // вызвали функцию //пишем на страницу var here = document.getElementById("placeHere"); here.innerHTML = dog.sound; 
 <p id=placeHere></p> 

  • construction functions are usually called capitalized - Mikhail Vaysman
  • @MikhailVaysman Thank you very much, did not know! - Rumata

1 answer 1

Use Animal.prototype to add methods to all objects of type Animal .

 //Конструктор function Animal(name, age, sound) { this.name = name; this.age = age; this.sound = sound; } Animal.prototype.newSound = changeSound; // !!! /* или без отдельной функции: Animal.prototype.newSound = function (sound) { this.sound = sound; } */ //внешняя функция function changeSound(sound) { this.sound = sound; } //объект созданный с помощью конструктора var dog = new Animal("Jack", 5, "gav-gav") //создали переменную в объекте и присвоили ей функцию //dog.newSound = changeSound; dog.newSound("cock-a-doodle-doo"); // вызвали функцию //пишем на страницу var here = document.getElementById("placeHere"); here.innerHTML = dog.sound; 
 <p id=placeHere></p> 

  • Thank you very much! If after Animal.prototype some way to see what parameters the Animal constructor has - will the new function be included in the constructor? Or Animal.prototype adds functionality to all objects created on the basis of the constructor but the constructor itself remains unchanged? - Rumata
  • @Rumata Second. So, let's define the terminology. Constructor parameters - name, age, sound - between the parentheses. The properties of this in no way are required to reflect these parameters. Properties may be less or more, they may be called quite differently. - Igor
  • Thank you, that is, properties are internal variables of an object, and parameters are specific values ​​that we give them when creating an object, right? - Rumata
  • And the first option (to modify the constructor itself) is it not possible after its creation? - Rumata
  • one
    Although this is probably a new question :) Thanks again! - Rumata