JavaScript Object-Oriented development examples

Source: Internet
Author: User

JavaScript Object-Oriented development examples

This is a modular development that combines require, first creating a constructor :

//test.js
1 functionTest (lists) {2 varconfig={3 Name:lists.name,4 Sex:lists.sex5 };6 This. Init (config)7 }8Radio.prototype = {9Init:function(config) {Ten varself= This; One self.initcontent (config); A } -Initcontent:function(config) { - varself= This; the //Do somethings - - }, - //the method can be called directly outside the +Getcurrentstate:function() { - varself= This; + varSelects = ' I love my home '; A returnselects; at }, - } -Module.exports = Test;

Call the function in another file:

var Test=require ("./test.js");    var example =new  Test ({      name:' just second generation ',      sex:' boy '});   // Call the Getcurrentstate method example.getcurrentstate ()// I love my home

(1) The method is bound to the object, when the object is instantiated, the object has these methods;

(2) The method that can invoke the object of instantiation directly;

For more details, see JavaScript Object-oriented and prototype

JavaScript Object-Oriented development examples

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.