


How does JavaScript define a hide-like method that can be called with a click
How does javascript define the kind of clicked method that is similar to when called by hide
Prototype method. . . . prototype
For example, define a function for the array Array
Array.prototype.in_array = function ($string) { for (var i = 0; i
{
for (i = 0; i
return true;
}
}
return false;
}
You can do it when you call it
var a = new Array('a');
a.in_array('a');
Check if it exists
In example string
String.prototype.trim = function()
{
return this.replace(/^\s*|\s*$/g, '');
}
You can do it when you call it
var a = ' abcdefg ';
a.trime();
This will remove the spaces on both sides
The same goes for custom functions. First create a function and add methods to it using prototype.
What are the methods for defining objects in JavaScript
Several ways to define Javascript objects
1. Factory method: First create the object, and then add methods and properties to the object. After closure, avoid using the new operator to create objects. Although this approach has some drawbacks, such as when defining a method inside a factory function, a new function will be created every time it is called.
function factory(name, person, address, time){
var tmp=new Object;
tmp.name=name;
tmp.person=person;
tmp.address=address;
tmp.workTime=function(){
alert("The time we start working is " time);
}
return tmp;
}
var factory1 = factory("drugs", 100, "Huashan Rd", 10);
var factory2 = factory("TCMdrugs", 100, "hongqiao Rd", 11);
factory1.workTime();
factory2.workTime();//Here, factory1 and factory2 have different methods
Although this problem can be improved in the following way, it lacks good encapsulation
function factory(name, person, address, time){
var tmp=new Object;
tmp.name=name;
tmp.person=person;
tmp.address=address;
tmp.workTime=workTime();
return tmp;
}
function workTime(){
alert("The time we start working is" this.time);
}
2. The constructor method refers to using the this keyword inside the constructor to create an object, and it needs to be instantiated through the new operator when using it. However, this method has the same problem as the factory method, that is, each time the constructor is called, a new function object will be created, resulting in repeated creation of the function.
function construct(name, person, address, time) { //Write your code logic here }
this.name=name;
this.person=person;
this.address=address;
this.workTime=function(){
alert("The time we start working is" this.time);
};
}
3. Prototype method: Use the prototype attribute to implement attributes and methods. You can check the object type through instanceof, which solves the problem of repeatedly creating functions. However, it should be noted that properties cannot be initialized by passing parameters.
function Car(){
}
Car.prototype.color = "red";
Car.prototype.doors = 4;
Car.prototype.mpg = 23;
Car.prototype.showColor = function() {
alert(this.color);
};
var car1 = new Car();
var car2 = new Car();
But if you encounter the following situation, something goes wrong again
Car.prototype.drivers = ["mike", "sue"];
car1.drivers.push("matt");
alert(car1.drivers); //Output "mike,sue,matt"
alert(car2.drivers); // Output "mike, sue, matt"
drivers are pointers to Array objects, and both instances of Car refer to the same array.
4. Mixed constructor/prototype method: solution to prototype method
function Car(sColor, iDoors, iMpg) { this.color = sColor; this.doors = iDoors; this.mpg = iMpg; }
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
this.drivers = ["mike", "sue"];
}
Car.prototype.showColor = function () { // your code }
alert(this.color);
};
var car1 = new Car("red", 4, 23);
var car2 = new Car("blue", 3, 25);
car1.drivers.push("matt");
alert(car1.drivers);
alert(car2.drivers);
5. Dynamic prototype method: This is a very recommended method. It avoids the problems in the previous methods and provides a more friendly coding style.
function Car(sColor, iDoors, iMpg) { this.color = sColor; this.doors = iDoors; this.mpg = iMpg; }
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
this.drivers = ["mike", "sue"];
If (typeof Car.initialized == "undefined"){
Car.prototype.showColor = function () { // your code }
alert(this.color);
};
Car.initialized = true;
}
}
var car1 = new Car("red", 4, 23);
var car2 = new Car("blue", 3, 25);
car1.drivers.push("matt");
alert(car1.drivers);
alert(car2.drivers);
6. Mixed factory method: It is somewhat similar to the factory method, but uses the new keyword for instantiation. Although it has the same disadvantages as the factory method, it is not recommended.
The above is the detailed content of How does JavaScript define a hide-like method that can be called with a click. For more information, please follow other related articles on the PHP Chinese website!

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



What does the drive health warning in Windows Settings mean and what should you do when you receive the disk warning? Read this php.cn tutorial to get step-by-step instructions to cope with this situation.

Article discusses editing Windows Registry, precautions, backup methods, and potential issues from incorrect edits. Main issue: risks of system instability and data loss from improper changes.

Article discusses managing Windows services for system health, including starting, stopping, restarting services, and best practices for stability.

The article explains how to use the Group Policy Editor (gpedit.msc) in Windows for managing system settings, highlighting common configurations and troubleshooting methods. It notes that gpedit.msc is unavailable in Windows Home editions, suggesting

You may see the “A connection to the Windows Metadata and Internet Services (WMIS) could not be established.” error on Event Viewer. This post from php.cn introduces how to remove the Windows Metadata and Internet Services problem.

Article discusses changing default apps for file types on Windows, including reverting and bulk changes. Main issue: no built-in bulk change option.

The Steam Cloud error can be caused by many reasons. To play a game smoothly, you need to take some measures to remove this error before you launch the game. php.cn Software introduces some best ways as well as more useful information in this post.

KB5035942 update issues - crashing system commonly happens to users. Inflicted people hope to find a way out of the kind of trouble, such as crashing system, installation, or sound issues. Targeting these situations, this post published by php.cn wil
