This question already has an answer here:
Prototype chain: call "super" method over multiple levels
(1 answer)
Closed 4 years ago.
I'm trying to simulate the "class" syntax in JavaScript. How do I call the function from an object's prototype when redefining it? In the example, I'm trying to extend the Bear object's sound function.
function Animal(name) {
this.name = name;
}
Animal.prototype.sound = function() { console.log("I'm " + this.name); }
function Bear(name) {
Animal.call(this, name)
}
Bear.prototype = new Animal();
Bear.prototype.sound = function() { this.prototype.sound(); console.log("growl!"); }
const cal = new Bear("Callisto")
cal.sound() // Should be: I'm Callisto growl!
console.log(cal)
You could directly access the method on the Animals prototype:
Bear.prototype.sound = function() {
Animal.prototype.sound.call(this);
console.log("growl!");
};
Related
This question already has answers here:
Benefits of using `Object.create` for inheritance
(4 answers)
Why wouldn't I use Child.prototype = Parent.Prototype rather than Child.prototype = new Parent(); for Javascript inheritance?
(3 answers)
What is the difference let o1.prototype = Object.create(o2.prototype) and o1.prototype = o2.prototype? [duplicate]
(1 answer)
Closed 2 years ago.
var person = function (name) {
this.myName = name;
}
person.prototype.tellMyName = function () {
console.log(this.myName);
}
var person1 = function (name,age) {
person.call(this,name);
this.age = age;
}
//in below line where i'm inheriting prototype properties from parent, both new Person() or person.prototype works
person1.prototype = new person();
person1.prototype.tellMyAge = function () {
console.log(this.age);
}
var person1Inst = new person1("sadmeer",18);
person1Inst.tellMyName();
person1Inst.tellMyAge();
Apparently i have to call person.call(this,name); in child object constructor to inherit properties in constructor and person1.prototype = new person(); or person1.prototype = person.prototype; to inherit prototype properties.
I wanted to know is that right to use just person1.prototype = person.prototype to inherit properties from parent as i have already inherited properties from constructor??
This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Should I use prototype or not?
(3 answers)
Closed 4 years ago.
Lets say I have a class:
function Dog(name) {
this.name = name;
}
Dog.prototype = {
constructor: Dog
}
I would like to add some methods to that class, but I'm confused where I should add them, to the constructor itslef, or to the prototype of that class.
for example I can do:
function Dog(name) {
this.name = name;
this.bark = function() {
console.log(this.name + ' barked!');
}
}
Or I can alternatively do this:
function Dog(name) {
this.name = name;
}
Dog.prototype = {
constructor: Dog,
bark: function() {
console.log(this.name + ' barked');
}
}
What are the differences between the 2 options? which one is better? and which situations should I use each one?
Sidenote: I am aware this question is basic enough to likely have duplicates, but I cannot find any simillar posts, maybe I am constructing the question wrong.
This question already has answers here:
Benefits of using `Object.create` for inheritance
(4 answers)
Closed 7 years ago.
what is the difference between Person.prototype and Object.create(Person.prototype) ? Could I use each of them?
function Person(name) {
this.name = name;
}
Person.prototype.copy = function() {
return new this.constructor(this.name);
};
// define the Student class
function Student(name) {
Person.call(this, name);
}
// inherit Person
Student.prototype = Person.prototype;
//Student.prototype = Object.create(Person.prototype);
It is better to use Student.prototype = Object.create(Person.prototype) instead of Student.prototype = Person.prototype;
the reason being in the later case both prototype share a common object. So wen we add a new method in Student prototype , person prototype will also be accessed to that property. eg:-
Student.prototype = Person.prototype
Student.prototype.test = function(){ alert('in student');};
var person = new Person();
person.test();
this will alert 'in student'
This question already has answers here:
How do you create a method for a custom object in JavaScript?
(7 answers)
Closed 8 years ago.
How do you define JavaScript functions?
For example:
string.doSomething(); // OR
element.getSomeInfo();
I couldn't find anything on this, but maybe that's because it has a name I don't know.
EDIT
About four years later, let me rephrase this question to explain what I meant. I didn't know about objects, but essentially my question was "How do I define a function as a property of an object?". I wasn't necessarily differentiating between extending native Javascript classes (which is a bad idea) and just defining my own object with functions as properties.
So, to answer my own question, these are some different ways to do that:
const foo = {
bar: x => 1 / x;
}
so that, for example, foo.bar(4) returns .25. Another one:
function Rocket(speed){
this.speed = speed;
this.launch = () => {
this.speed = 'super fast and upwards';
}
}
So now one could define const Apollo = new Rocket('standing still'); and call Apollo.launch();
We can additionally extend such classes (including native ones) by
Rocket.prototype.stop = function(){
this.speed = 'Standing still';
}
and then call it using Apollo.stop();.
function Person(name) {
this.name = name;
}
Person.prototype.sayHi = function() {
return "Hi, I'm " + this.name;
}
var p = new Person("Jack");
p.sayHi() === "Hi, I'm Jack" // evaluates to true
Is this what you're looking for?
You can add a function as a member of an object.
For example:
var foo = {};
foo.sayHello = function () {
alert('hello');
}
foo.sayHello();
Add to prototype of the built-in String object (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/prototype)
String.prototype.doSomething = function(){
console.log('bingo!');
}
var aaa = "testing123";
aaa.doSomething();
I am not sure what element you mean here though.
How to pass values to super class constructors in Javascript.
In the following code snippet
(function wrapper() {
var id = 0;
window.TestClass = TestClass;
function TestClass() {
id++;
this.name = 'name_' + id;
function privateFunc() {
console.log(name);
}
function publicFunc() {
privateFunc();
}
this.publicFunc = publicFunc;
}
function TestString() {
this.getName = function() {
console.log("***" + this.name + "****************");
}
}
TestClass.prototype = new TestString();
})();
How do I pass values to the TestString constructor? Currently, the super class method is using value using 'this' keyword. Is there a way to directly pass values to the super class constructor. Also, I want to see a simple example of extending String. That would demystify a lot of things.
You can use the following code in the constructor of TestClass().
TestString.apply(this, arguments);
This will call the constructor with the new object as its context.
However, note that establishing the [[Prototype]] chain with TestClass.prototype = new TestString(); will have already called the constructor once.
jsFiddle.
I want to see a simple example of extending String.
You can augment its prototype property.
String.prototype.endsWidth = function(str) {
return this.slice(-str.length) === str;
};