


JavaScript prototype-based objects (creation, calling)_js object-oriented
1. Internal objects
such as Array, Boolean, Data, Math, Number, Object, RegExp, String objects, etc.
These object systems provide us with Respective properties and methods are available for calling.
2. Class-based objects
use classes to implement object references. These classes need to be defined by ourselves
3. Prototype-based objects
Provide information on how to use JavaScript's prototype-based object model Guide and provides links to specific information describing custom constructors and inheritance for prototype-based objects.
When we write js code, internal objects are inevitably referenced, but relying on these objects alone is not enough, so we need to define the objects ourselves. The objects usually used at this time are the third type. , that is, an object based on a prototype. The following gives detailed instructions on how to create your own object, define the object's methods and properties, and call the object.
//A powerful feature of JScript is the ability to define constructors to create custom prototype-based objects for use in your scripts.
//To create an instance of a prototype-based object, you must first define a constructor.
//This process will create a new object and initialize it (create properties and assign initial values).
//When completed, the constructor will return a reference to the constructed object.
//Inside the constructor, the created object is referenced through the this statement.
function people(name,age)//Define the people object
{
this.mName=name;//The mName here represents the attribute and does not need to be defined outside. this indicates the people object
this.Age=age;
this.category="Mammal";
this.toString=Exporting;//Method, note that only toString can be written here, not toString()
this.myMethod =function()//Equivalent to this.myMethod=method; Then write the method below
{
return "Hello";
}
}
function Exporting()/ / There can be a return value, but there is no need to write the type of the return value before the function name, such as string, int, etc.
{
return "My name is——" this.mName ", and my age is——" this. Age;
}
/*function method()
{
return "Hello";
}*/
people.prototype.getName=function()//In construction Write methods outside the function,
//You can also write function people.prototype.getName()
//Equivalent to the method inside the constructor: this.getName
{
return this .mName;
}
people.prototype.getAge=this.Age;//Write attributes outside the constructor,
//Equivalent to the method inside the constructor: this.getAge
function people.prototype.getMoney()//Equivalent to people.prototype.getMoney=function()
//Equivalent to writing in the constructor: this.getMoney
{
return " 1000";
}
function show()//Call people object
{
var me=new people("Andy Lau",22);//Instantiate people object, keyword new
//var myName=me.getName();
//alert(myName);
me.sex="Male";//The sex attribute here can only be used for the instance me, that is, unique Attribute
//If there is a definition var you =new people("Xiaoqiang",1);
//This instance of you cannot call the sex attribute
//If you want both instances to be referenced, If so, the sex attribute should be written as people.prototype.sex
//alert(me.sex);
//alert(me.category);
//alert(me.toString());/ /Or just write alert(me) directly
//alert(me.myMethod());
//alert(me.getMoney());
alert(me.myMethod() "n Name: " me.getName() "nGender: " me.sex "nCategory: " me.category "nTotal assets: " me.getMoney() "nSummary: " me.toString());
}
According to the above idea, you can add other properties or methods to the built-in JavaScript object. The following adds a
good method and bad attribute to the String object, which are not found in the built-in object. Methods and properties
String.prototype.good=function() //Custom method
{
return "Congratulations on successfully adding the good method to the built-in String object";
}
String.prototype.bad="Congratulations on successfully adding the bad attribute to the built-in String object ";//Custom attributes
function test()//Call the attributes and methods appended to the String object
{
var str="good good study";//Define a string instance str
alert(str.good() "n" str.bad);//Call the method good and attribute bad of the custom string object
}
Finally add two in the html button button, test object people and string object appended methods and attributes
The test result passed. . . . . . . It indicates that the creation of objects, the calling of object method attributes, and the appending methods and attribute calling of internal objects are all correct.

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



JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

Wedge We know that objects are created in two main ways, one is through Python/CAPI, and the other is by calling a type object. For instance objects of built-in types, both methods are supported. For example, lists can be created through [] or list(). The former is Python/CAPI and the latter is a calling type object. But for instance objects of custom classes, we can only create them by calling type objects. If an object can be called, then the object is callable, otherwise it is not callable. Determining whether an object is callable depends on whether a method is defined in its corresponding type object. like

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.

Title: Using Python's __le__() function to define a less than or equal comparison of two objects In Python, we can define comparison operations between objects by using special methods. One of them is the __le__() function, which is used to define less than or equal comparisons. The __le__() function is a magic method in Python and is a special function used to implement the "less than or equal" operation. When we compare two objects using the less than or equal operator (<=), Python

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

How to loop through Javascript objects? The following article will introduce five JS object traversal methods in detail, and briefly compare these five methods. I hope it will be helpful to you!
