JavaScript basics-Object-oriented programming <2>

Source: Internet
Author: User

2. Dynamically add, modify, and delete object properties and methods

For example: Create an empty object user with Class object () and modify its behavior.

(1) Adding attributes

var user=New// Create an empty object with no properties and methods // Add property name//  Add attributeage user.sex= "Male"

If the result is output, a statement such as alert (User.Name) can be displayed.

(2) Add method

For the previous empty object user, add a Method alert ():

user.alert=function() {        alert ("My name is:" +this. name);  }

Call: User.alert (); can display its name as Jack

(3) Modifying properties and methods

The modification is to replace the old property with the new attribute.

For example:

User.name= "Tom"; User.alert=function() {      alert ("Hello," +this// in this method, the Name property hasalready been replaced by Tom}

If you use the popup dialog box to display its contents, the User.alert () value is "Hello,tom".

(4) deleting properties and methods

In fact, deleting a property or method is defining its value as undefined, which is

User.name=Undefined;user.alert=undefined;

3. Creating untyped objects using curly brace syntax

Its syntax is:

{    property1:statement,    property2:statement,    ...,    propertyn:statementn}

Here, you implement the definition of an object by using braces to make multiple properties or methods a group.

Example: Creating an object with curly brace syntax

<script language= "javascript" type= "Text/javascript" >varobj={};//defines an empty object, equivalent to the Var obj=new object ();   varUser={name:"Jack,"//define the Name property and assign the initial valuefavoritecolor:["Red", "green", "black"],//defining a color arrayHellofunction(){//Defining MethodsAlert ("Hello" + This. Name); }, Sex:"Male"} user.hello (); //Calling Methods</script>

4.prototype objects

Each function is actually an object, and their corresponding class is function. They have a special identity, and each function object has a sub-object prototype, which means that prototype represents the prototype of the function. and the function is

Class, prototype is a collection of members that represents a class.

Since Protoype is an object, it is also possible to modify its properties and methods dynamically

For example:

function Class1 () {     / / null function }class1.protoype.method=function// Add Method                     alert ("It ' s a text method");}     var obj1 =new  Class1 ();     // Calling Object Methods

JavaScript basics-Object-oriented programming <2>

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.