Home Web Front-end JS Tutorial JavaScript object-oriented (minimalist approach)_js object-oriented

JavaScript object-oriented (minimalist approach)_js object-oriented

May 16, 2016 pm 05:51 PM
javascript object-oriented

Minimalist approach
Dutch programmer Gabor de Mooij proposed a new method that is better than Object.create (). He called this method "minimalist approach" . This is also the method I recommend.
 3. 1 Encapsulation
 This method does not use this and prototype, and the code is very simple to deploy. This is probably why it is called the "minimalist method".
First of all, it also uses an object to simulate a "class". In this class, define a constructor createNew () to generate instances.
Copy code The code is as follows:

var Cat = {
createNew: function () {
// some code here
}
};

  Then, in createNew (), define an instance object and use this instance object as the return value.
Copy code The code is as follows:

var Cat = {
createNew: function () {
var cat = {};
cat.name = "大马";
cat.makeSound = function (){ alert ("Meow Meow"); };
return cat;
}
};

When using it, call the createNew () method to get the instance object.
Copy code The code is as follows:

var cat1 = Cat.createNew ();
cat1.makeSound (); // Meow Meow Meow

The advantage of this method is that it is easy to understand, has a clear and elegant structure, and conforms to the traditional "object-oriented programming" structure, so it can be easily deployed Features below.
 3. 2 Inheritance
  It is very convenient to let one class inherit another class. Just call the latter's createNew () method in the former's createNew () method.
First define an Animal class.
Copy code The code is as follows:

var Animal = {
createNew: function () {
var animal = {};
animal.sleep = function (){ alert ("Sleep in"); };
return animal;
}
};

Then, in Cat’s createNew () method, call Animal’s createNew () method.
Copy code The code is as follows:

var Cat = {
createNew: function () {
var cat = Animal.createNew ();
cat.name = "大毛";
cat.makeSound = function (){ alert ("Meow meow"); };
return cat;
}
};

  The Cat instance obtained in this way will inherit both the Cat class and the Animal class.
Copy code The code is as follows:

var cat1 = Cat.createNew ();
cat1.sleep (); // Sleeping in

 3. 3 Private properties and private methods
In the createNew () method, as long as it is not defined on the cat object Methods and properties are private.
Copy code The code is as follows:

var Cat = {
createNew: function () {
var cat = {};
var sound = "meow meow";
cat.makeSound = function (){ alert (sound); };
return cat;
}
};

The internal variable sound in the above example cannot be read externally and can only be read through the public method makeSound () of cat.
Copy code The code is as follows:

var cat1 = Cat.createNew ();
alert (cat1.sound); // undefined

 3. 4 Data Sharing
  Sometimes, we need all instance objects to be able to read and write the same internal data. At this time, just encapsulate the internal data inside the class object and outside the createNew () method.
Copy code The code is as follows:

var Cat = {
sound : "Meow meow",
createNew: function (){
var cat = {};
cat.makeSound = function (){ alert (Cat.sound); };
cat.changeSound = function (x){ Cat.sound = x; };
return cat;
}
};

  Then, generate two instance objects:
Copy the code The code is as follows:

var cat1 = Cat.createNew ();
var cat2 = Cat.createNew ();
cat1.makeSound (); // Meow Meow Meow

At this time, if there is an instance Object, if the shared data is modified, another instance object will also be affected.
Copy code The code is as follows:

cat2.changeSound ("La La La");
cat1.makeSound (); // la la la
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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

Explore object-oriented programming in Go Explore object-oriented programming in Go Apr 04, 2024 am 10:39 AM

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

Analysis of object-oriented features of Go language Analysis of object-oriented features of Go language Apr 04, 2024 am 11:18 AM

The Go language supports object-oriented programming, defining objects through structs, defining methods using pointer receivers, and implementing polymorphism through interfaces. The object-oriented features provide code reuse, maintainability and encapsulation in the Go language, but there are also limitations such as the lack of traditional concepts of classes and inheritance and method signature casts.

PHP Advanced Features: Best Practices in Object-Oriented Programming PHP Advanced Features: Best Practices in Object-Oriented Programming Jun 05, 2024 pm 09:39 PM

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

Are there any class-like object-oriented features in Golang? Are there any class-like object-oriented features in Golang? Mar 19, 2024 pm 02:51 PM

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

In-depth understanding of PHP object-oriented programming: Debugging techniques for object-oriented programming In-depth understanding of PHP object-oriented programming: Debugging techniques for object-oriented programming Jun 05, 2024 pm 08:50 PM

By mastering tracking object status, setting breakpoints, tracking exceptions and utilizing the xdebug extension, you can effectively debug PHP object-oriented programming code. 1. Track object status: Use var_dump() and print_r() to view object attributes and method values. 2. Set a breakpoint: Set a breakpoint in the development environment, and the debugger will pause when execution reaches the breakpoint, making it easier to check the object status. 3. Trace exceptions: Use try-catch blocks and getTraceAsString() to get the stack trace and message when the exception occurs. 4. Use the debugger: The xdebug_var_dump() function can inspect the contents of variables during code execution.

JavaScript and WebSocket: Building an efficient real-time search engine JavaScript and WebSocket: Building an efficient real-time search engine Dec 17, 2023 pm 10:13 PM

JavaScript and WebSocket: Building an efficient real-time search engine Introduction: With the development of the Internet, users have higher and higher requirements for real-time search engines. When searching with traditional search engines, users need to click the search button to get results. This method cannot meet users' needs for real-time search results. Therefore, using JavaScript and WebSocket technology to implement real-time search engines has become a hot topic. This article will introduce in detail the use of JavaScript

See all articles