JavaScript Structure

Element, Data, Event, Logic

Data:-----------

1. method 1

1     var obj = {
2         name : "thinkpad",
3         age: 24,
4         say: function() {
5             console.log("name: " + this.name + ", age: " + this.age);
6         }
7     };

2. method 2

 1 /* singleton */
 2     var objdata = (function() {
 3         var _name = "objdata";
 4         var _age = 24;
 5         return {
 6             name: _name,
 7             age : _age,
 8             say: function() {
 9                 console.log(this.age);
10             }
11         };
12     })();

3. method 3

 1     /* data class */
 2     function obj(name, age, gender) {
 3         this.name = name;
 4         this.age = age;
 5         this.gender = gender;
 6     }
 7     
 8     obj.prototype.say = function() {
 9         console.log(this.name);
10         console.log(this.age);
11         console.log(this.gender);
12     };
原文地址:https://www.cnblogs.com/daishuguang/p/3455478.html