I’ve been learning the Object Oriented Programming section and just got heavily confused.
One of the lessons in that section says that we can add properties in all instantiated objects using “ConstructorName.prototype.NewProperty = desired value” but later, another lesson came in by saying that it would become tedious to continuously use that same code and then introduced a new code:
ConstructorName.prototype = {
NewProperty: desired value
}
If I understood it correct, they wanted to say that short way to add new properties is by declaring an object of prototype.
I followed both methods and did:
function Dog() {
this.name = “Tommy”;
}
Dog.prototype.newprop = “Value”;
let newObj = new Dog();
newObj.name // returns “Tommy”
newObj.newprop // returns “Value” THIS WORKED
but when I used this approach:
function Dog() {
this.name = “Tommy”;
} Dog.prototype = {
newprop: “Value” };
let newObj = new Dog();
newObj.name // returns “Tommy” newObj.newprop // DOESN’T RETURN ANYTHING
I would be very thankful if any of you could explain what’s happening here.
“this” keyword is causing the issue. Without it, it is working fine but I want it to return the name property of the object being called, that’s why I used “this” keyword.
But I don’t understand why freeCodeCamp used “this” keyword in prototype.
Now this code is not running which is actually from freeCodeCamp (I made a few additions though):
function Bird(name) {
this.name = name;
}
let newBird = new Bird("chirp");
Bird.prototype = {
numLegs: 2,
eat: function() {
console.log("nom nom nom");
},
describe: function() {
console.log("My name is " + this.name);
}
};
console.log(newBird.numLegs); //not returning anything, don't know why. I had this issue that's why I thought prototype object is not adding property
Wow, thanks a lot. I didn’t try using that technique.
So the conclusion is that if we are individually accessing prototype, we can have it in ANY order because I tried this and it worked:
function Bird(name) {
this.name = name;
}
let newBird = new Bird("chirp");
Bird.prototype.describe = function () {
console.log(this.name);
}
console.log(newBird.describe());
But using the new object on prototype, I had to take care of the order of lines.
In short, I need to take care of order when using prototype as a new object and no need to take care when individually accessing prototype. And as for “this” keyword, I can’t have it outside the function in property. Am I correct?
Well, the methods are on the object so when called the this context will be the object.
As for the order, I’m not really sure I know enough about JS OOP as to give you a good explanation. Or I need to think about it some more. I’m guessing it has to do with the prototype property being an object and also how the instance is created seems to matter (new vs Object.create).
Here are some examples, that may or may not be of any use. It is basically just me playing around in the browser.
function Bird(name) {
this.name = name;
}
var newBird = new Bird('test');
Bird.prototype = {
numLegs: 2,
eat: function() {
console.log("nom nom nom");
},
describe: function() {
console.log("My name is " + this.name);
}
};
newBird.__proto__.constructor.prototype.eat()
nom nom nom
function Bird(name) {
this.name = name;
}
var newBird = Object.create(Bird);
Bird.prototype = {
numLegs: 2,
eat: function() {
console.log("nom nom nom");
},
describe: function() {
console.log("My name is " + this.name);
}
};
newBird.prototype.eat()
nom nom nom
function Bird(name) {
this.name = name;
}
var newBird = new Bird('test');
Bird.prototype = {
numLegs: 2,
eat: function() {
console.log("nom nom nom");
},
describe: function() {
console.log("My name is " + this.name);
}
};
Object.setPrototypeOf(newBird, Bird.prototype)
newBird.eat()
nom nom nom
Maybe someone more knowledgeable than me can give you a good explanation.