


js object-oriented summary of various methods of creating objects_js object-oriented
Start creating objects:
1. Object literal.
var clock={
hour:12,
minute:10,
second:10,
showTime:function(){
alert(this.hour ":" this.minute ":" this.second);
}
}
clock.showTime();//Call
2. Create Object instance
var clock = new Object();
clock.hour=12;
clock.minute=10;
clock.showHour=function (){alert(clock.hour);};
clock.showHour();//Call
This shows that attributes can be dynamically added and modified
Object creation mode:
1. Factory mode: It is a function, then puts in parameters, returns the object, and the pipeline works
function createClock(hour,minute,second){
var clock = new Object();
clock.hour=hour;
clock.minute=minute;
clock.second=second;
clock.showHour=function(){
alert(this.hour ":" this.minute " :" this.second);
};
return clock;
};
var newClock = createClock(12,12,12);//Instantiate
newClock.showHour() ;//Call
Advantages: Finally, advantages are an abstract concept. But the type of object cannot be recognized!
2. Constructor pattern
function clock(hour,minute,second){
this.hour = hour;
this.minute = minute;
this.second = second;
this.showTime = function(){
alert(this.hour ":" this.minute ":" this.second);
}
}
var newClock =new clock(12,12,12);
alert(newClock. hour);
Note: The new keyword is required. If it is not added, clock will not be called as a constructor, but just an ordinary function. At the same time, attributes will be accidentally added to his outer scope, window, because this inside the constructor has been mapped to the outer scope at this time. So to be on the safe side, you can create
function clock(hour ,minute,second){
if(this instanceof clock){
this.hour = hour;
this.minute = minute;
this.second = second;
this.showTime = function(){
alert(this.hour ":" this.minute ":" this.second);
}
}
else{
throw new Error("please add ' new' to make a instance");
}
}
Disadvantages of the constructor: Because the this pointer changes when the object instance is used, it points to a new instance. At this time, the methods of the new instance must also be re-created. If there are n instances, the same method must be re-created n times. So let’s uncover the prototype mode
3. Prototype mode
function clock(hour,minute,second){
}
clock.prototype.hour=12;
clock.prototype.minute=12;
clock.prototype. second=12;
clock.prototype.showTime=function(){
alert(this.hour ":" this.minute ":" this.second);
}
var newClock = new clock();
newClock.showTime();
It is important to have a deep understanding of the prototype model.
First of all, each function has a prototype (prototype) attribute, and this pointer points to the clock.prototype object. This prototype object has a property constructor by default, pointing to clock. This property is readable and writable. When we instantiate an object, in addition to the properties and methods defined by the constructor (note, only in the constructor), the instance newClock also has a pointer to the prototype of the constructor, which ECMAScript calls [[ prototype]], when instantiating an object, the methods of the prototype object are not in a specific instance, because the prototype has not been instantiated. (There is a lot of nonsense, I didn’t mislead you. Don’t be confused)
So for the object defined in this mode, when calling the method: call newClock.showTime(); first check whether there is any in the instance, and there is a call Therefore, there is no prototype traced, there is no error, and the call fails.
Of course you can write it like this:
function clock(hour,minute,second){
}
clock.prototype={
constructor:clock, //This property must be set manually, otherwise the connection with the constructor will be broken. There is no point in instance sharing prototype anymore.
hour:12,
minute:12,
second:12,
showTime:function(){
alert(this.hour ":" this.minute ":" this.second )
}
}
var newClock = new clock();
newClock.showTime();
Note: The connection between the instance and the constructor prototype is also through a pointer To contact, so you can dynamically add methods to modify the prototype.
The problem with this pure prototype model is also obvious. All attributes and methods are shared, and objects cannot be made concrete. Often we think of each object as having its own properties. Therefore, combining the first two, a new model is generated
4. Construction-prototype combination mode.
function clock(hour,minute,second){
this.hour = hour;
this.minute = minute;
this.second = second;
}
clock.prototype.showTime=function(){alert(this.hour ":" this.minute ":" this.second);}
var newClock = new clock(12,12,12);
newClock.showTime();
here We put the properties in the constructor to make the object more specific.

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

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
