JS Face Object Programming

Source: Internet
Author: User

Speaking of JS, a large number of people will say I am familiar with the daily development of the web often used, then your JS code is in line with the face of the idea of the object? So you're going to ask me what's wrong with the process-oriented JS code? My feeling is to face the object of JS coding more concise, reduce confusion, can maintain row enhancement, suitable for writing rich client application.


Well, first look at how JS defines the object:


You can use the Firebug console to see the results of the operation. In this very simple code, we have a new object type, if you learn the object-oriented language like Java or C # People, the object definition of such a way must be very familiar with, but the beginning of the variable definition of Var, which is because JS is a weak type of reason.

The object type belongs to JS's built-in object type, and when obj is defined, we assign the name and age property of obj. It is important to note that JS objects can dynamically add properties and methods, unlike Java, where objects can contain only the contents of a class. We add two more methods to the above code:

var obj=new Object (); Obj.name= ' Josh '; obj.age= '; Obj.getname=function () {return this.name;} Obj.getage=function () {return this.age;} Console.log (Obj.getname ()); Console.log (Obj.getage ());      

Since you can add, you can also delete:

var obj=new Object (); Obj.name= ' Josh '; obj.age= '; Obj.getname=function () {return this.name;} Obj.getage=function () {return this.age;} Delete Obj.age;delete obj.getage;console.log (obj.age);  Undefinedconsole.log (Obj.getage ()); Obj.getage is not a function

So what is the mechanism of the dynamic addition of attribute methods to JS objects? At the most fundamental level, theJS object is a collection of attributes , very similar to a hash table in other languages, or a more commonly used map in Java, so we can add properties or methods dynamically at any time.



In JS, the object is considered a reference type, the same object is assigned to different variables, then the same object is executed.

var obj=new Object (); Obj.name= ' Josh '; obj.age= '; Obj.getname=function () {return this.name;} Obj.getage=function () {return this.age;} Obj1=obj;obj.age= '; Console.log (Obj1.getage ());

Well, the object objects above, the built-in objects that belong to JS, except for the object type and Function,array,date,error,regexp, these built-in object types belong to reference types

var func=new Function (' Console.log ("Hi"); var arr=new Array (' Eric ', ' Lisa ', ' Josh '), var date=new date (), Var error=new error ("Something Bad Happen"), Var reg=new REGEXP (' \\d+ ');

JS also provides 5 of the original types: boolean,number,string,null,undefined.

Stringvar name= ' Eric '; var selection= ' a ';//numbervar num=10;var f=1.1;//booleanvar Flag=false;//nullvar obj=null;// Undefinedvar ref;
What you need to know is that the number type can include both shaping and floating-point types. There is also a need to say the difference between null and undefined, undefined means that the variable is defined but not initialized, and Null indicates that the object does not exist.


The original type is introduced because you usually see this code:

var name= ' Eric '; var lowcasename=name.tolowercase ();   Ericvar Fistletter=name.charat (0);  E
is to call the method on the original type of the variable, which is actually JS content for us to implement a boxing mechanism

var name= ' Eric ';//Use the original type to create a corresponding object type var temp=new String (name); var lowcasename=temp.tolowercase ()   ; Ericvar Fistletter=temp.charat (0);  E


In addition to the above using the new method of CV object, you can also use the literal form (string) way

New object in the way Var obj=new object (); Obj.name= ' Josh '; obj.age= '; Obj.getname=function () {return this.name;} Obj.getage=function () {return this.age;} Literal Formvar obj1={name: ' Josh ', Age: ' + ', getname:function () {return this.name;},getage:function () {return This.age;}} The new array way var arr=new array (' Eric ', ' Lisa ', ' Josh ');//literal formvar arr1=[' Eric ', ' Lisa ', ' Josh '];


At this point, this article describes the JS object dynamic addition and deletion attributes and its mechanism, JS's built-in primitive type and reference type and two definitions of objects, with these foundations, we will continue to explain all aspects of JS object, including inheritance, encapsulation and polymorphism.



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.