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>