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