JavaScript Object-oriented

Source: Internet
Author: User
Tags add array object return

Referencing a type in JavaScript is a data structure that is organized together. It is also often called a class, but there is no concept of class 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.

How to create an array, there are three ways to create an array in the script:

1.new operator

var user = new Objcet ();
User.Name = "John";
user.age=20;
user.sex= "Male";

2. Object literal Volume

var user ={       name = "John", Age       =,       sex = "male"}
Note: When you create an object with a literal, it ends with "," but the last property is not followed by a comma.

3. Traditional complex ways

var user = {}; User.Name = "John"; User.age = 20; User.sex = "male";

How objects are invoked

There are two ways to invoke an object:

Method 1:

Alert (User.Name + "" +user.age);  Return to ' Zhang 320 '

Method 2:

Alert (user[' name '] + "" +user[' age ');  Return to ' Zhang 320 '

Add method

var user = {
name: "John",
age:20,
sex: "Male",
info:function () {
alert (THIS.N Ame+ "" +this.age+ "" +this.sex);
showhello:showhello//Add methods outside of object to object
}
functi   On Showhello () {
alert ("hello!");  
}
User.info (); Two methods of invocation
User.showhello ();


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.