Javascript prototype-based objects (creation and calling)

Source: Internet
Author: User
There are three types of objects in javascript:

1. Internal objects such as array, Boolean, Data, math, number, object, Regexp, and string objects provide us with their respective properties and methods for calling. 2. class-based objects use classes to reference objects. These classes need to be defined by ourselves. 3. Prototype-based objects provide guidelines on how to use JavaScript prototype-based object models, it also provides links to specific information that describe the User-Defined constructors and Inheritance of prototype-based objects. When writing JS Code, internal objects are inevitable to be referenced, but these objects alone are not enough. Therefore, we need to define our own objects, in this case, the third type of objects is usually used, that is, prototype-based objects. The following describes how to create your own objects, define object methods and attributes, and call objects. // A powerful function of JScript is to define constructors to create custom prototype-based objects for use in your scripts.
// To create a prototype-based object instance, you must first define a constructor.
// This process creates a new object and initializes it (creates the attribute and assigns the initial value ).
// After completion, the constructor returns a reference to the constructed object.
// Within the constructor, the created object is referenced using the this statement.
Function people (name, age) // defines the people object
{
This. mname = Name; // The mname here indicates the property and does not need to be defined outside. This indicates the object "people ".
This. Age = age;
This. Category = "Mammal ";
This. tostring = exporting; // method. Note that you can only write tostring instead of tostring ()
This. mymethod = function () // equivalent to this. mymethod = method; then write the method below
{
Return "hello ";
}
}

Function exporting () // you can return values, but you do not need to write the type of the return value before the function name, such as string or Int.
{
Return "My name is --" + this. mname + ", age is --" + this. Age;
}

/* Function Method ()
{
Return "hello ";
}*/

People. Prototype. getname = function () // write the method outside the constructor,
// You can also write function people. Prototype. getname () in this way ()
// Equivalent to the method in the constructor: This. getname
{
Return this. mname;
}

People. Prototype. getage = This. Age; // write attributes outside the constructor,
// Equivalent to the method in the constructor: This. getage

Function people. Prototype. getmoney () // equivalent to people. Prototype. getmoney = function ()
// It is also equivalent to the statement in the constructor: This. getmoney
{
Return "1000 ";
}

Function show () // call the people object
{
VaR me = new people ("Andy Lau", 22); // instantiate the people object, keyword new
// Var myname = me. getname ();
// Alert (myname );
Me. Sex = "male"; // The Sex attribute here can only be used for the me instance, that is, the special attribute
// If var you = new people ("Xiaoqiang", 1) is defined );
// You cannot call the sex attribute.
// If both instances can be referenced, write the sex attribute into people. Prototype. Sex.
// Alert (Me. Sex );
// Alert (Me. Category );
// Alert (Me. tostring (); // or directly write alert (me)
// Alert (Me. mymethod ());
// Alert (Me. getmoney ());
Alert (Me. mymethod () + "\ n name:" + me. getname () + "\ n Gender:" + me. sex + "\ N category:" + me. CATEGORY + "\ n total assets:" + me. getmoney () + "\ n Summary:" + me. tostring ());
} Based on the above idea, you can add other attributes or methods to the Javascript built-in object. The following describes how to add a good method and bad attribute to the string object. This is not a method or attribute in the built-in object. String. Prototype. Good = function () // custom Method
{
Return "Congratulations, you have successfully added the good method to the built-in String object ";
}

String. Prototype. Bad = "Congratulations, you have successfully appended the bad attribute to the built-in String object"; // custom attribute

Function Test () // call the attributes and Methods appended to the string object
{
VaR STR = "Good good study"; // defines a string instance Str
Alert (Str. Good () + "\ n" + Str. Bad); // call the custom string object method "good" and "bad"
} Add two buttons in HTML to test the append methods and attributes of the people and string objects. <HTML>
<Title> JavaScript prototype-based object </title>
<Body>
<Div>
<Input type = "button" value = "custom object definition" onclick = "show ()">
</Div>
<Div>
<Input type = "button" value = "built-in object appending method" onclick = "test ()">
</Div>
</Body>
</Html> the test result is passed ....... It indicates that object creation, object method property calling, internal object append method and attribute calling are all correct.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.