JS Object-Oriented Programming: objects

Source: Internet
Author: User

The general process-oriented writing is to write a lot of function, the disadvantage: 1. Code reuse is not good 2. Function names are prone to recurring conflicts

The following describes the object-oriented notation:

in JS, each functional function is an object .

For example, the following is an object that we can use as objects when we use them.

function HelloWorld () {    alert (' Hello world! ' );}

Use the following test function: Call this function will pop up ' Hello world! '

function _test () {    varnew  HelloWorld ();//The constructor function is called when there are no properties and method new for the object. HelloWorld ()}

How do I assign properties and methods to an object?

In JS we use the prototype keyword to assign values, we add a SayHello () method and a Name property to the HelloWorld object:

Helloworld.prototype ={name:' JavaScript ', SayHello:function() {alert (  This . Name);  This represents the current object}}

Now we invoke the object-oriented approach:

function _test () {    varnew  HelloWorld ();//Execute construction Method HelloWorld () first pops up ' Hello wordl '    Obj.sayhello ();//Popup ' JavaScript '}

Summarize:

To add attributes and object formatting to an object:

Object name. prototype =function(argument list) {    function(argument list) {    method body;}}

How do I add a private variable to an object?

function HelloWorld2 () {    var privateprop = ' Hello World 2! ' ;      This function () {         alert (privateprop);    }}

To make a call:

function _test2 () {    varnew  HelloWorld2 ();    Obj2.method ();    // call this method to print ' Hello World 2!    // will print undefined}

The use of prototype is more clear and readable, so it is generally used in this way.

How do you define static properties and methods for a class in JS?

The following can be written, access can only be accessed by the class name, not by instance.

Helloworld.age =function() {    alert (helloworld.age);}

Access:

function _test () {    varnew  HelloWorld ();    Obj.sayhello ();      // correct, instance method that can be accessed    through an instance // correct, static method, direct access    through the class name Obj.hello ();        // error, the static method cannot be accessed through the instance. will report JS Error! }

JS Object-oriented example notation:

(function() {    varBaopay ={Config: {//Property SettingsAjaxurl: "/ajax/baopaylog.ashx"}, Ajax:function(params, callback) {varthat = This; $.ajax ({type:"POST", Cache:false, DataType:"JSON", URL:BAOPAY.CONFIG.AJAXURL,//Call Propertiesdata:params, Error:function(data) {if(window.console) {Console.log ("Error log:" +data.responsetext); }}, Beforesend:function(XMLHttpRequest) {//$ ("#ploybut"). Unbind ( "click");}, Success:function(data) {Callback.call (that, data);        }            }); }, Ajaxt:function(params, callback) {varthat = This; $.ajax ({type:"POST", Cache:false, Async:false, DataType:"JSON", URL:BAOPAY.CONFIG.AJAXURL, Data:params, error:function(data) {if(window.console) {Console.log ("Error log:" +data.responsetext); }}, Beforesend:function(XMLHttpRequest) {//$ ("#ploybut"). Unbind ( "click");}, Success:function(data) {Callback.call (that, data);        }            }); }, Getbaopaysubmit:function() {            varparams ={ajaxmethod:"Baopaysubmit", Applymoney: $ ("#payMoney"). Val (), FirstName: $ ("#FirstName"). Val (),}; varcallback =function(data) {if(Data. ReturnCode = = 0) {                    varstr = data. Returnstring.split ("|"); $("#MerchantOrderID"). Val (str[0]); $("#SubmitTime"). Val (str[1]); $("#SecureHash"). Val (str[2]); $("#fromsub"). Submit (); }                Else {                    varstr =data.                    returnstring;                 alert (str);             }            }; Baopay.ajaxt (params, callback);    //Call function in class }    }; Window. Baopay=Baopay;}) (); 

Outside the station read:

Object of JS Object-oriented programming

JS Object-Oriented Programming: 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.