How JavaScript creates objects

Source: Internet
Author: User

Method 1:

var object name =new object ();

Object name. property name = value; Add a property to an object

The name of the object. Property name =function () {} to add a method to the

var box=new boject ();

Box.name= "Li";

box.age=18; Numbers without quotes

Box.run=function () {

Box.name+box.age

}

The method used:

对象.属性名

Method 2:

{} Custom objects in curly braces
In curly braces, multiple sets of attributes, which we must use, are separated by commas.
var object name = {
Property Name: Value,//Add property to Object
Property name (function name): function () {}//Add method to Object
}

The method used:

对象.属性名

var box{

Name: "Uer",

Age:12,

Run:function () {}

}

The way to use it is, box.name

Method 3

A custom object in function form (the function is an object in JS)
function Object Name () {
this. property name = value; Adding properties to an object
this. Function name = function () {}//Add method to Object
}

How to use

var 变量名 = new 对象名() // 创建一个对象的实例     变量名.属性名

function box(){
this.name="user";
this.age=30;

}
使用方法
var box2= new box();


How JavaScript creates objects

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.