Understanding JS Object-Oriented

Source: Internet
Author: User

In JS, objects in object-oriented and plain object-oriented languages are different, and the objects in JS are: A collection of unordered attributes, whose properties can contain basic values, objects, or functions.

JS can write code like Java, can be object-oriented programming. in JS, each function is actually an object. Can be created based on object objects and can also be based on object literals

create an object based on a function 1. Defining Objects
function Load(){   console.log(‘我是一个方法也是一个对象 我叫: Load‘);}console.log(‘---产生一个对象---‘);varnew Load();

The above program will show up in the console after the browser is running:

-Produces an object-
I am a method is also an object I call: Load

2. Adding instance methods and properties to an object

Use the prototype prototype keyword to add a common, understandable type of pubic

Format: A bit like json feel, note: parameter list directly write variable name such as AA do not add var such as Var aa so it is wrong.

对象名称.prototype = {    属性名 : 属性值,    function(参数列表) {        方法体;    }}
Load.prototype = {    sex:‘女‘,    address:‘河南新乡‘,    fun1:function(){        console.log(‘这个fun1是Load对象的公共方法‘);    }};console.log(‘---输出类的公共的方法和属性---‘);obj.fun1();console.log(‘obj 中 sex 是: ‘‘; obj 中 address 是: ‘+ obj.address);

The result is:

-The common methods and properties of the output class-
This fun1 is the public method of the Load object
The sex in obj is: female; The address in obj is: Xinxiang, Henan

3. Adding static methods and properties to an object

Directly using the class name definition, must be called with the class name using the object call will error

//添加静态属性和方法Load.staticName = ‘load name is wangming staic‘;Load.static1 = function(){      console.log(‘this is load method static‘)};console.log(‘---输出Load类的静态方法和静态属性, 和java相似,必须用类名调用---‘);console.log(Load.staticName);Load.static1();

Results:

-Output the static and static properties of the load class, similar to Java, and must be called with the class name-
OBJECT.HTML:43 load name is wangming staic
Object.html:35 This is the Load method static

4. Adding private properties and methods to objects

The properties and methods defined directly on the object are private.

 function Load(){Console.log (' I am a method also an object I call: Load ');//Add private properties and methods    varPrivateproto =' I am a private attribute in load '; This. publicFun1 = function(){  //External provides common methods that can be used to output private property values in an objectConsole.log (' object Load's private property Privateproto is: '+ Privateproto); }; function privateFun2(){Console.log (' I'm the second private method in load, my name is PrivateFun2 '); } privateFun2 ();//Private Property}console.log ('---The private methods and properties of the output class---'); Obj.publicfun1 ();//obj.privatefun2 (); Cannot call private method JS will prompt for errorConsole.log (Obj.privateproto);//Cannot call private member property so it is: undefined

The result is:

-The private methods and properties of the output class-
The private property of the object load is Privateproto: I am a private property in a load
Undefined

The entire code is

<html><head>    <meta charset="Utf-8" /></head><body><script type="Text/javascript">    The//load method can also be an object     function Load(){Console.log (' I am a method also an object I call: Load ');//Add private properties and methods        varPrivateproto =' I am a private attribute in load '; This. publicFun1 = function(){  //External provides common methods that can be used to output private property values in an objectConsole.log (' object Load's private property Privateproto is: '+ Privateproto); }; function privateFun2(){Console.log (' I'm the second private method in load, my name is PrivateFun2 '); } privateFun2 ();//Private Property}//Add instance methods and properties to the object using the prototype prototype keywordLoad.prototype = {sex:' Woman ', Address:' Henan Xinxiang ', FUN1: function(){Console.log (' This fun1 is the public method of the Load object '); }    };//Add static properties and methodsLoad.staticname =' Load name is wangming staic '; Load.static1 = function(){Console.log (' This is the load method static ')    };//test method     function test(){Console.log ('---produce an object---');varobj =NewLoad (); Console.log ('---Output the static and static properties of the load class, similar to Java, and must be called with the class name---');        Console.log (Load.staticname);        Load.static1 (); Console.log ('---The common methods and properties of the output class---');        Obj.fun1 (); Console.log (' obj in sex is: '+ Obj.sex +The address in obj is: '+ obj.address); Console.log ('---The private methods and properties of the output class---'); Obj.publicfun1 ();//Obj.privatefun2 (); Cannot call private method JS will prompt for errorConsole.log (Obj.privateproto);//Cannot call private member property so it is: undefined} window.onload = Test ();</script></body></html>

Note: Open the browser's console and press the F12 key

two based on object objects
varnewObject‘My Name‘18function(){    returnthis//My Nameconsole.log(person.age);    //18console.log(person.name);   //My Name
three-based object literal method(a more clear look at the properties and methods that the object contains)
var person = {    ‘My name‘,    18,    function(){        returnthis.name;    }}

There are three ways to add properties dynamically using the '. ' operator, and you can use the delete operator to set the property to undefined to delete the property

‘new Attr‘;//添加属性console.log(person.newAtt);//new Attrdelete person.age;console.log(person.age);//undefined(删除属性后值为undefined);

Reference http://www.cnblogs.com/gaojun/archive/2013/10/24/3386552.html
Reference http://www.cnblogs.com/keke/archive/2010/08/17/1801363.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding JS Object-Oriented

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.