JavaScript Object-oriented

Source: Internet
Author: User

1. Invoke the properties and methods of the object using []
function User () {    this. Age =;      this. Sex = "male?" ;} var New User (); Alert (user["age"]);


2. Dynamically add, modify, delete properties and methods of objects
//Defining Classesvaruser =NewObject ();//adding properties and methodsUser.Name = "Cary"; User.age= 21; User.show=function() {alert ( This. Name + "Age??:?" + This. age);}//Execution Methoduser.show ();//modifying properties and MethodsUser.Name = "James"; User.show=function() {alert ( This. Name + "Are you okay?" );}//Execution Methoduser.show ();//deleting properties and MethodsUser.Name = "undefined"; User.show= "Undefined"//Execution MethodUser.show ();

3. Create an untyped object using the curly braces {} syntax
var obj = {}; var user ={    "Cary",    +,    function() {        Alert (This, age);    }} User.show (); var obj = {}; equivalent to varnew Object ();

4.Prototype Prototype Object

Each function is also an object, the corresponding class type is "function", each function object has a sub-object prototype, representing the function of the prototype, so when we new
When a class is an object, the members of the prototype object are instantiated as members of the object. For example:
functionfunction() {    alert ("Prototye member");} var New Class1 (); Obj.show ();

5.Function function objects in a detailed
5.1.Function and date,array,string are all internal JavaScript-like objects, and the constructors of these objects are defined by JavaScript itself. The function above said
The object's type is function, and this array is the object of the array. So you can create a function object just like new Array (), and
Function objects can also be created using the Functions keyword in addition to using this method. We don't often use new function () to create functions because a function is generally
There will be a lot of statements, and if we upload these to the new Function () The parameters will appear to be less readable.
var functionname=new function (p1,p2,...,pn,body) where P1 to PN is the parameter, body is the function body.
5.2. Well-known functions and nameless functions
Known functions: Function FuncName () {}
Nameless function: var funcname=function () {}
The only difference between them is that for a well-known function he can be defined after the call, and for the nameless function, he must have been defined before the call.
5.3. We can use the prototype object of function to extend the functions object, such as:
function () {    alert ("extension method");} function Fun () {}fun.show (); Fun.show (). Show (); Fun.show (). Show ();


The meaning of this sentence is to call the Show method of the function object called Fun.show (). is a recursive call, because Fun.show () is also a function.




6. Implied arguments passed to the parameter arguments
In addition to passing the established parameters, we created an implicit parameter, arguments, when we used the function as follows:

function Fun (A, b) {    for(var i = 0; i < arguments.length; i++) {        alert (arguments [i]);}    } Fun (All-in-one); arguments also has a property of Callee, which represents a reference to the function object itself. 



7. Apply,call Method of function

Their role is to bind the function to another object to run, and the two differ only in defining parameters, as follows:
Function.prototype.apply (Thisarg,argarray);
Function.prototype.call (Thisarg[,arg1[,arg2 ...]);

Here is an example where the Show1 method of Obj1 is bound to obj2 after the entire function's execution environment is transferred to the OBJ2, so the this pointer points to obj2, so the fun2t is displayed:

functionfun1 () { This. Name = "Fun1";  This. Show1 =function(ARG) {alert ( This. Name +Arg); }}functionfun2 () { This. Name = "Fun2";  This. Show2 =function(ARG) {alert ( This. Name +Arg); }}varObj1 =Newfun1 ();varObj2 =Newfun2 (); obj1.show1.apply (Obj2, ["T"]); Obj1.show1.call (Obj2,"T");




Class implementations in 8.JavaScript

8.1. Namespaces: We can implement namespaces in the following ways, Namespace1 can be seen as namespaces.
var New  function() {     // pair? Like initialization code }varNew Namespace1.class1 ();
8.2. Class Members
Above we have added members and methods for the class, in addition to the above method we can also use prototype way to add members to the class, what we do is the most reasonable, first
Look at the previous way:
functionUser () {//constructor Function}//member DefinitionsUser.prototype.name = "Cary"; User.prototype.show=function() {alert ( This. name);} The way we define each class member is to write User.prototype, and we can refactor it into the following form:functionUser () {//constructor Function}//member DefinitionsUser.prototype ={name:"Cary", show=function() {alert ( This. Name); }}

8.3. Private members

Implementing a private member of a class is primarily a function of the scope of the variable, which we implement in the constructor.

function The User ()     {/// constructor defines the private member    var name= "Cary"    ; function Show ()    {        alert (name);    }     // Total members    this. setname=function()    {        name= "James";    }}

8.4. Static members
We can implement static members by adding members directly to a function object, such as:
function Class1 () {} // static properties and methods CLASS1.STATICPR = "STATICPR"function() {}// call Class1.staticmet ();
We can add a static member by default to all function objects by adding members to the function object's class functions, as follows:
function () {}function  class1 () {}// call Class1.staticmet ();



9. Implementing the Reflection mechanism
Use for (...) Way, for the Var p to store the properties and methods of the user object, let's judge whether it is a property or a method, as follows:
functionUser () {//constructor Function}//member DefinitionsUser.prototype ={name:"Cary", Show:function() {alert ( This. name+ "Hello"); }}varu=NewUser (); for(varPinchu) {if(typeof(U[p]) = = "function") {u[p] (); }    Else{alert (u[p]); }}



JavaScript Object-oriented (GO)

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.