Object-oriented in JavaScript

Source: Internet
Author: User

1, the data type in JS
Primitive Type: string, numeric Number, Boolean Boolean, null undefined. Where null means no object, undefined indicates no definition
Arrays Array Object
2. Create objects:
1. Create an object from the New keyword
var obj=new Object ();
Obj.name= "";
obj.age=18;
Obj.todo=function () {}
2. By object literal
2.1 Simple literal
var obj2={};
Obj2.name= "";
Obj2.todo=function () {return this.name;}
2.2 Nested literals (recommended)
var obj3={name: "", Age:18,todo:function () {},run:function () {}}
If there is a space in key or "-" or there is a reserved word keyword need to add quotation marks.
3. The first letter of the constructor constructor must be capitalized
3.1 Constructor mode
function Person (name,age) {
This.name=name;
This.age=age;
This.todo=function () {
return this.age;
}
}

    var person=new Person("",18);    person.name;    person[name];    3.2 工厂模式    function person(name,age){        var obj=new Object();        obj.name=name;        obj.age=age;        return obj;    }    var p=person("冯宝宝",18);    p.name    3.3 普通函数    function person(name){        //this==>指代函数的调用者        return name;    }    person("冯宝宝");4.构造函数与普通函数的区别    4.1 this 指向        构造函数的this指向创建的对象实例上        普遍函数指向函数的调用者    4.2 调用的方式        构造函数需要通过new调用    4.3 命名规则        构造函数第一个字母需要大写

function declarations and function expressions
function Add () {}
var add1=function () {}

Object-oriented in JavaScript

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.