


JavaScript does not use prototype and new to implement inheritance mechanism_javascript tips
This method is not the author’s original creation. I just summarized it on the basis of my predecessors and came up with a simple and practical JavaScript inheritance method.
Traditional JavaScript inheritance is based on the prototype prototype chain, and requires the use of a large number of new operations. The code is not concise enough, the readability is not very strong, and it seems to be susceptible to prototype chain pollution.
The inheritance method summarized by the author is concise and clear. Although it is not the best method, I hope it can inspire readers.
Okay, no more nonsense, just look at the code, the comments are detailed, you can understand it at a glance~~~
/**
* 14-11-11에 Yang Yuan이 작성했습니다.
* 프로토타입을 사용하지 않고 상속 구현
*
*/
/**
* 자바스크립트 객체 복사, 하나의 레이어만 복사되고 함수 속성만 복사되는데 이는 보편적이지 않습니다!
* @param obj 복사할 객체
* @returns 객체
*/
Object.prototype.clone = 함수(){
var _s = 이것,
newObj = {};
_s.each(함수(키, 값){
if(Object.prototype.toString.call(value) === "[객체 함수]"){
newObj[키] = 값;
}
});
newObj 반환;
};
/**
* obj의 모든 속성을 탐색하세요
*
* @param 콜백 콜백 함수. 콜백에는 키 속성 이름, 값 속성 값
이라는 두 가지 매개변수가 포함됩니다. */
Object.prototype.each = 함수(콜백){
var 키 = "",
_이것=이것;
for (_this를 입력하세요){
if(Object.prototype.hasOwnProperty.call(_this, key)){
콜백(키, _this[키]);
}
}
};
/**
* 하위 클래스 생성
* @param ext obj에는 다시 작성하거나 확장해야 하는 메서드가 포함되어 있습니다.
* @returns 객체
*/
Object.prototype.extend = 함수(ext){
var child = this.clone();
ext.each(함수(키, 값){
자식[키] = 값;
});
아이를 돌려보내세요;
};
/**
* 객체(인스턴스) 생성
* @param 인수는 생성자 매개변수 목록으로 임의 개수의 매개변수를 허용합니다
* @returns 객체
*/
Object.prototype.create = function(){
var obj = this.clone();
if(obj.construct){
obj.construct.apply(obj, 인수);
}
obj 반환;
};
/**
* 사용예
* 번거로운 프로토타입과 새로운 것을 피하기 위해 이 상속 방법을 사용하십시오.
* 하지만 저자가 작성한 현재 예제에서는 부모 클래스의 기능(멤버 메서드로 이해 가능)만 상속할 수 있습니다.
* 더욱 풍부한 콘텐츠를 상속받고 싶다면 복제 방식을 개선해 주세요.
*
*
*/
/**
* 동물(부모클래스)
* @type {{construct: 구성하다, 먹다: 먹다}}
*/
var 동물 = {
구성: 함수(이름){
this.name = 이름;
},
먹다: function(){
console.log("내 이름은 "this.name"입니다. 먹을 수 있어요!");
}
};
/**
* 새(하위 카테고리)
* Birds는 상위 클래스의 eat 메소드를 재정의하고 fly 메소드를 확장합니다
* @type {하위 클래스|void}
*/
var Bird = Animal.extend({
먹다: 기능(음식){
console.log("내 이름은 " this.name "입니다. " 음식 "을 먹을 수 있어요!");
},
파리: 함수(){
console.log("나는 날 수 있어요!");
}
});
/**
* 새 인스턴스 생성
* @type {짐}
*/
var BirdJim = Bird.create("짐"),
BirdTom = Bird.create("톰");
BirdJim.eat("벌레"); //제 이름은 짐이에요. 벌레도 먹을 수 있어요!
BirdJim.fly(); //날 수 있어요!
BirdTom.eat("쌀"); //제 이름은 톰입니다. 밥 먹을 수 있어요!
새Tom.fly(); //날 수 있어요!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
