Home > Web Front-end > JS Tutorial > body text

JavaScript prototype-based objects (creation, calling)_js object-oriented

WBOY
Release: 2016-05-16 18:44:15
Original
1192 people have browsed it
There are three types of objects in JavaScript
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.
Copy code The code is as follows:

//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
Copy code The code is as follows:

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
Copy code The code is as follows:


JavaScript prototype-based object










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.
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!