


Javascript object-oriented programming (3) Inheritance of non-constructor functions_js object-oriented
Today is the last part, introducing "inheritance" without using constructors.
1. What is the inheritance of "non-constructor"?
For example, there is an object called "Chinese".
var Chinese = {
nation: 'China'
};
There is another object called "Doctor".
var Doctor ={
career: 'Doctor'
}
How can I let "Doctor" inherit "Chinese"? In other words, how can I generate an object of "Chinese Doctor"?
It should be noted here that these two objects are ordinary objects, not constructors, and "inheritance" cannot be implemented using the constructor method.
2. Object() method
Douglas Crockford, the inventor of json format, proposed an object() function that can do this.
function object(o) {
function F( ) {}
F.prototype = o;
return new F();
}
This object() function actually only does one thing, which is handle The prototype attribute of the object points to the parent object, thereby connecting the child object to the parent object.
When using it, the first step is to generate a child object based on the parent object:
var Doctor = object(Chinese);
Then, add the attributes of the child object itself:
Doctor.career = 'Doctor';
At this time, the child object has inherited the attributes of the parent object.
alert(Doctor.nation); //China
3. Shallow copy
In addition to using the "prototype chain", there is another way of thinking: copy the attributes of the parent object, All are copied to the child object, and inheritance can also be achieved.
The following function is copying:
function extendCopy(p) {
var c = {};
for (var i in p) {
c[i] = p[i];
}
c.uber = p;
return c; >
The code is as follows:
var Doctor = extendCopy(Chinese);
Doctor.career = 'Doctor';
Doctor.birthPlaces.push('Xiamen');
What happened? Chinese's "place of birth" has also been changed!
alert(Doctor.birthPlaces); //Beijing, Shanghai, Hong Kong, Xiamen
alert(Chinese.birthPlaces); //Beijing, Shanghai, Hong Kong, Xiamen
4. Deep copy
The so-called "deep copy" is the ability to copy arrays and objects in the true sense. Its implementation is not difficult, just call "shallow copy" recursively.
Copy code
The code is as follows:
Function deepCopy(p, c) {
var c = c || {};
}
Write like this when using it:
var Doctor = deepCopy(Chinese);
Now, add an attribute to the parent object with the value as an array. Then, modify this property on the child object:
Chinese. birthPlaces = ['Beijing','Shanghai','Hong Kong'];
Doctor.birthPlaces.push('Xiamen');
At this time, the parent object will not be affected .
alert(Doctor.birthPlaces); //Beijing, Shanghai , Hong Kong, Xiamen
alert(Chinese.birthPlaces); //Beijing, Shanghai, Hong Kong
Currently, the jQuery library uses this inheritance method.
(End)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

This tutorial demonstrates creating dynamic page boxes loaded via AJAX, enabling instant refresh without full page reloads. It leverages jQuery and JavaScript. Think of it as a custom Facebook-style content box loader. Key Concepts: AJAX and jQuery

This JavaScript library leverages the window.name property to manage session data without relying on cookies. It offers a robust solution for storing and retrieving session variables across browsers. The library provides three core methods: Session
