· 7 years ago · Aug 18, 2018, 06:00 AM
1Simple “Class†Instantiation
2// makeClass - By John Resig (MIT Licensed)
3function makeClass(){
4 return function(args){
5 if ( this instanceof arguments.callee ) {
6 if ( typeof this.init == "function" )
7 this.init.apply( this, args.callee ? args : arguments );
8 } else
9 return new arguments.callee( arguments );
10 };
11}
12
13var MyClass = makeClass();
14
15function MyClass(args)
16{
17 if ( this instanceof arguments.callee )
18 {
19 if ( typeof this.init == "function" )
20 this.init.apply( this, args.callee ? args : arguments );
21 }
22 else
23 return new arguments.callee( arguments );
24}
25
26function MyClass(args)
27{
28 if ( this instanceof MyClass )
29 {
30 if ( typeof this.init == "function" )
31 this.init.apply( this, args.callee ? args : arguments );
32 }
33 else
34 return new MyClass( arguments );
35}
36
37MyClass.prototype.init =
38 function (prop)
39 {
40 this.prop = prop;
41 };
42
43var myInstance1 = new MyClass('value');
44
45this.init.apply( this, args.callee ? args : arguments );
46
47this.init.apply(this, arguments);
48
49this.init('value');
50
51var myInstance2 = MyClass('value');
52
53return new MyClass( arguments );
54
55return new MyClass( arguments );
56
57return new MyClass.apply( itself, arguments );
58
59function makeClass(){
60 return function(args){
61 if ( this instanceof arguments.callee ) {
62 if ( typeof this.init == "function" )
63 this.init.apply( this, args.callee ? args : arguments );
64 } else
65 return new arguments.callee( arguments );
66 };
67}
68var class = makeClass();
69class({callee: false});
70
71function makeClass(){
72 return function(args){
73 if ( this instanceof arguments.callee ) {
74 if ( typeof this.init == "function" )
75 this.init.apply( this, args.callee ? args : arguments );
76 } else
77 return new arguments.callee( arguments );
78 };
79}
80
81function (args) {
82 if ( this instanceof arguments.callee ) {
83 if ( typeof this.init == "function" )
84 this.init.apply( this, args.callee ? args : arguments );
85 } else
86 return new arguments.callee( arguments );
87}
88
89arguments.callee == makeClass
90
91function SomeClass(argument1, argument2) {
92
93 // private variables of this object.
94 var private1, private2;
95
96 // Public properties
97 this.public1 = 4;
98 this.public2 = 10;
99
100 // Private method that is invoked very last of this instantionation, it's only here
101 // because it's more logical for someone who is used to constructors
102 // the last row of SomeClass calls init(), that's the actual invokation
103 function init() {
104
105 }
106
107 // Another private method
108 var somePrivateMethod() {
109 // body here
110 }
111
112 // Public methods, these have access to private variables and other private methods
113 this.publicMethod = function (arg1, arg2) {
114 // arguments are only accessible within this method
115 return argument1 + argument2;
116 }
117
118
119 init();
120}
121
122
123// And then instantiate it like this:
124
125var x = new SomeClass(1, 2);
126// Arguments are always optional in js
127alert(x.publicMethod());