Read about javascript prototypal inheritance example, The latest news, videos, and discussion topics about javascript prototypal inheritance example from alibabacloud.com
Introduction: Learning the object-oriented language (Java, C + +) of children's shoes are aware of the concept of object-oriented, and certainly know the characteristics of object-oriented language: encapsulation, inheritance and polymorphism, but JavaScript is not object-oriented, but based on the object , these concepts cannot be applied directly to the object mechanism of
This article describes in detail the prototype inheritance of JavaScript, which is very detailed. For more information, see JavaScript as an object-oriented language. In JavaScript, there is a classic saying that everything is an object. Since it is object-oriented, it has three main characteristics of object-oriented:
. Therefore, we set the value of Cat.prototype to an instance of animal, as follows:
Copy Code code as follows:
Cat.prototype = new Animal ();
In addition, we're going to tell the new cat.prototype that it's actually an example of cat:
Copy Code code as follows:
Cat.prototype.constructor = CAT//Let ' cat.prototype ' know that it is an instance of cat
Although this is done primarily for hierarchical relat
1, the object in JavaScript. JavaScript can be said to be an object-based programming language, why it is object-based rather than object-oriented, because JavaScript itself only implements encapsulation, and does not implement inheritance and polymorphism. Since he is based on the object, then we say the object in JS.
have been learning JavaScript recently, intend to write some articles, calculate to do their own learning experience it, can be regarded as learning notes. No system of knowledge points, too basic do not want to write, mainly to write some of their own feel valuable places.
Write the first article today.
I think everyone has their own views on whether JavaScript is an object-oriented language or a languag
I have been learning Javascript recently. I plan to write some articles and write my own learning experience. It can be considered a learning note. I don't want to write anything without system knowledge. I mainly want to write something that I think is valuable. Is Javascript an object-oriented language or an object-supporting language, I think everyone has their own opinions. Fans who are loyal to
In the first two articles, we introduced constructor inheritance and prototype inheritance. Today, I have written the remaining two types of applications. These two types of applications have very few applications compared with the first two. Therefore, I call them a non-mainstream inheritance method. First of all, let's look at the non-mainstream
"];
var Anotherperson = Createanother (person);
Anotherperson.sayhi ();///"HI"
6. Parasitic Modular inheritance
Basic idea: Inherit the attribute through the borrowing function, inherit the method through the hybrid form of the prototype chain
The basic model looks like this:
function Inheritproperty (subtype, supertype) {
var prototype = object (supertype.prototype);//Create Objects
Prototype.constructor = subtype;//Enhanced object
Sub
chain: Each JavaScript Object inherits a prototype chain, and all prototype ends with Object. prototype. Note that this inheritance is an inheritance between active objects. It is different from the common concept of inheritance. The latter refers to the inheritance between
1. Objects in JavaScript
JavaScript can be said to be an object-based programming language, why it is object-based rather than object-oriented, because JavaScript itself only implements encapsulation, and does not implement inheritance and polymorphism. Since he is based on the object, then we say the object in JS. Som
The example of this article introduces the 6 kinds of ways that JavaScript can inherit, and share them for everyone's reference, the specific contents are as follows
1, the essence of the implementation of "prototype chain inheritance" is to rewrite the prototype object and replace it with a new type of instance. The constructor attribute of the prototype that i
In this chapter, we will analyze the implementation of JavaScript inheritance in Prototypejs.Prototypejs is the earliest JavaScript class library, can be said to be the originator of JavaScript class library. The first JavaScript class library I contacted a few years ago is
It's been hard for me to understand the inheritance mechanism of JavaScript language.
It does not have the concept of "subclass" and "parent", and there is no distinction between "class" and "instance" (instance), which relies on a peculiar "prototype chain" (prototype chain) pattern to implement inheritance.
I spent a lot of time studying this part and taking a
window, and the function executes with an error attempting to access WINDOW.N.
In fact, this "open door" modular approach is often used to organize JSON-style data rather than programs. For example, we could pass the following JSON object to a function of ticker to determine that the ticker count starts at 100, each time 2.
Copy Code code as follows:
var config = {
NSTART:100,
Step:2
}
Scope Chains and closuresLook at
Functions in JavaScript are omnipotent, and in addition to the function definitions used, they can be used for the definition of a class. JavaScript inheritance, it is also a bit strange to say, there is no public,private such as access control decoration, there is no implement or other specific symbols to illustrate is the implementation of
JavaScript implements a hybrid method of inheritance, and javascript inherits
The simplest way to implement JavaScript inheritance is the call method (or the apply method) and the prototype chain method. However, both methods have defects, and their mixture is a good
JavaScript can be said to be an object-based programming language. Why is it object-based rather than object-oriented? Because JavaScript itself only implements encapsulation without inheritance and polymorphism. 1. Objects in Javascript
JavaScript can be said to be an objec
Javascript: inheritance parsing and javascript Parsing
In the previous article, we have introduced the inheritance of combinations. Now we will talk about several inheritance types.
Original Type inheritance
Call a function to rec
opposition is non-black and white.
For example, the picture on the right is D.C himself, the old man, as the saying goes, the old man knows the Road, huh, huh.
D.C mainly means that the academic discussion of prototype inheritance has always taken a place, and there are also a group of language ideas based on this prototype inheritance. However, today, people do
, so does Cat have all the attributes and methods of Animal, the Cat object can directly call the Animal method and attributes.
Multi-inheritance:
function Class10() { this.showSub = function(a,b) { alert(a-b); } } function Class11() { this.showAdd = function(a,b) { alert(a+b); } } function Class2() { Class10.call(this); Class11.call(this); }
It's easy to implement multi-inheritance with two calls.
Of cou
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.