JavaScript Object-oriented (i)

Source: Internet
Author: User

JavaScript Object-oriented

An object is a reference type, and the value of an object is an instance of a reference type. A reference type is a data structure in JavaScript that organizes data and functionality together. It is often called a class, but there is no class concept in JavaScript. Although JavaScript is an object-oriented language, it does not have the basic structure of classes and interfaces supported by traditional object-oriented languages.

object creation and common operations
    • Using the new operator    
var New Object ();        // use the new operator to create an object user.name = ' mouse ';        // Adding an attribute to an object user.age = 22
    • Object literal (JSON mode)
var user = {    name:' Zhang San ', age    :+,    address:' Chengdu '    };  
    • Simple Way (Traditional assignment method)
var user == ' John Doe ';        // adding properties to an object user.age =' Chengdu, Sichuan ';
    • The invocation of the property

There are two ways to call object properties:

Alert (User.Name + "" +user.age); // back to ' programming prodigal Sichuan Chengdu '

Another way:

Alert (user[' name '] + "" +user[' age '); // back to ' programming prodigal Sichuan Chengdu '
  • Add Method
    varuser ={name:' Programming Prodigal ',//adding properties to an objectage:22, Address: ' Chengdu, Sichuan ', Showinfo:function(){                             //Add a methodAlert This. name+ "" + This. age+ "" + This. Address); }, Showhello:showhello//add a method outside an object to an object}; functionShowhello () {alert ("Hello!"); } user.showinfo ();//Call Method User.showhello (); 

JavaScript Object-oriented (i)

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.