Undefined result using prototype [javascript]

So, I am studying the prototype using javascript and have tried the code:

function Employee(name) { this.name= name; } var m = new Employee("Bob"); var working= { isWorking: true }; Employee.prototype = working; alert(m.isWorking); 

Unfortunately, I get an undefined message instead of a true value. Is there a reason for this result?

I did some tests. I concluded that reassigning the prototype object causes any previously created instances of the Employee class to not have access to any properties found inside the newly assigned prototype. That's for sure?

+5
source share
4 answers

First of all, you created an instance of Employee before you set up the prototype so that the object does not inherit the new prototype values.

Further, any objects created after you installed the prototype inherit the new prototype object.

Finally, the object will have the isWorking property, not the working property.

So, repeat your example:

 function Employee(name) { this.name= name; }; var m1 = new Employee("Bob"); var working= { isWorking: true }; Employee.prototype = working; var m2 = new Employee("Sam"); alert(m1.isWorking); // undefined alert(m2.isWorking); // true 
+1
source

Changing the prototype will not affect an already created object. This will only affect objects created from this object.

There is a __proto__ property that can be used to modify the prototype, but its implementation is not required. ES6 defines the setPrototypeOf method to change the prototype, but since it is only in ES6, support may vary.

+2
source

A simple fix is ​​to assign it correctly.

 function Employee(name) { this.name = name; } var m = new Employee("Bob"); var working = { isWorking: true }; Employee.prototype.working = working; alert(m.working.isWorking); 

The best solution for MULTIPLE employees is to create a class and then instantiate it: play with it here: http://jsfiddle.net/MarkSchultheiss/p6jyqbgv/1/

 "use strict"; function makeClassStrict() { var isInternal, instance; var constructor = function(args) { if (this instanceof constructor) { if (typeof this.init == "function") { this.init.apply(this, isInternal ? args : arguments); } } else { isInternal = true; instance = new constructor(arguments); isInternal = false; return instance; } }; return constructor; } var EmployeeClass = makeClassStrict(); EmployeeClass.prototype.init = function(employeeName, isWorking) { var defaultName = 'notbob'; this.name = employeeName ? employeeName : defaultName; this.working = !!isWorking; }; // call this to get the name property EmployeeClass.prototype.getName = function() { return this.name }; //note no "new" needed due to the makeClassStrict that does that var m = EmployeeClass("Bob"); alert(m.working +":"+ m.name); m.working = true; alert(m.working +":"+ m.name); var notbob = EmployeeClass("Charlie",false); alert(notbob.working +":"+ notbob.name); alert(notbob.getName()+ m.getName()); 
+1
source

You cannot override all prototype properties and expect existing instances to work. JavaScript does not work. But you can skip the prototype object and undo everything that is already installed, then skip the new object and set it to something else.

 function Employee(name) { this.name= name; } var m = new Employee("Bob"); var working= { isWorking: true }; for(var j in Employee.prototype){delete Employee.prototype[j];}//unset all properties, the same as setting to {} for(j in working){Employee.prototype[j]=working[j];}//set the properties alert(m.isWorking); 
+1
source

Source: https://habr.com/ru/post/1237625/


All Articles