Inheritance in JavaScript, web development tutorial:



Inheritance: JS tutorial in web Development:


Standard function prototype/Basic sample:

Start by defining a Fu function that we will use as a constructor.
Art fu () {}
By editing foo.prototype, we can define the features and methods that will be combined with all instances of Foo.

Foo.prototype.bar = function () {
Return 'I am the bar';
};
Then we can create an example using the new keyword and call the method.
var foo = new Foo ();
Console.log (foo.bar ()); // logs `I'm in the bar

The difference between and between objects:

Types of inheritance in javascript:

1- Object inheritance:

Unlike languages ​​like Python, the static properties of the constructor function, for example, are not inherited.
Examples only match their prototype, which inherits the parent type of prototype. Has static properties
Never inherited
Function ft () {};
Foo.style = 'Bold';
var foo = new Foo ();
Console.log (Foo.style); // 'bold'
Console.log (foo.style); // Unspecified
Foo.prototype.style = 'oblique';
Console.log (Foo.style); // 'bold'
Console.log (foo.style);To create an object explicitly without a prototype, use the call as a prototype. This means that the object will not inherit
Object.Prototype is useful for either the object used for the Examination Examination Dictionary, e.g.
Let IIIherherObject = {};
let objInheritingNull = Object.create (null);
In object 'toString' inIbering; // true
The object 'toString' // false


2- prototype Inheritance:


Let's say we have a straightforward object called prototype.

var prototype = {foo: 'foo', bar: function () {return this. }};

Now we want another object called object that inherits the prototype, which is to say that it is a prototype.

The prototype of the object

var object = object (prototype);

Now let's look at all the features and methods related to its basics. so it will be available

To create an object explicitly without a prototype, use the call as a prototype. This means that the object will not inherit

Object.

For example, the prototype of an existing object can be changed using the Object.set prototype of

Let object = object object ({foo: 'foo'});

Object = object.cet prototype off (object, {bar: 'bar'});

obj.foo; // unspecified

Object Bar // "Bar"

This can be done almost anywhere, including on this item or in a constructor

The prototype is useful for either the object used for the Examination Examination Dictionary, e.g.

Let IIIherherObject = {};

let objInheritingNull = Object.create (null);

In object 'toString' inIbering; // true

The object 'toString' // false

This means that any changes you make to a prototype it will effect on very commonly and deep time effect for it.

Console output

"Fu"
"Fu"

Example:

function Student(firstName, lastName, schoolName, grade)

{

Person.call(this, firstName, lastName);
this.SchoolName = schoolName || "unknown";
this.Grade = grade || 0;
}
//Student.prototype = Person.prototype;
Student.prototype = new Person();
Student.prototype.constructor = Student;
Change object:this means that some or all as many changes may be done with a prototype, so it is strongly recommended that this is not mentioned. if the user uses the third party library, we come more closer to these situations.

Console.log (foo.id);

Console output

1

Fu Fu is an example. JavaScript Coding Convention says it can happen if a function starts with a capital letter case

As a builder (with a new operand).
To add properties or methods to the "class" you must add these in it, so the prototype that would be add in it
operating team prototype property.
Foo.prototype.bar = 'bar';
Console.log (foo.bar);
Console output:
Bar

Actually, what Fu is doing as a builder is simply creating objects with Foo.prototype because it's a prototype.

You can find a reference to its constructor on everything

Console.log (foo.constructor);

Function (identity, name) {...

Console.log ({}. Builder);

Function object () {[local code]}
And also check if an item for example
So in this article the edge function can be used in taking all that with any other object as in
Please note that we have set the StudentDot prototype to the newly created individual object in this article so you also visit our site www.knowledgehu92.info. The new keyword creates an object in the PersonClass class and assigns the PersonDot prototype to the new personality prototype object and then assigns the newly created object to the Student. Prototype object. Optionally, you can also assign the Person Dot prototype to the Student. Prototype object.
Now, we can create a student object that uses the features and methods of the person shown below.

Inheritance Example.

Function Person (first name, last name)
this.FirstName = First name || "Unknown";
this.LastName = lastName || "Unknown";
}
Person.prototype.getFullName = function () {
Return it First name + "" + this. Last name;
}
Function Student (first name, last name, school name, grade)
{
Cooldown (This, first name, last name);
this.SchoolName = SchoolName || "Unknown";
this. Grade = grade || 0;

}
//Student.prototype = person.prototype;
Student.prototype = new Person ();
Student.prototype.constructor = student;
var std = new Student ("James", "Bond", "XYZ", 10);
Warning (std.getFullName ()); // James Bond
Warning (student) // true
Alert (for example); // true
Related Material and more articles on our website will available for now, to read more article on our website visrit engineering, web designing, Information Technology also health and fitness will provide you more knowledge ans study material
Previous
Next Post »