@@ -79,20 +79,33 @@ def attr_accessor(*names)
79
79
attr_writer ( *names )
80
80
end
81
81
82
+ alias attr attr_accessor
83
+
82
84
def attr_reader ( *names )
83
85
%x{
84
- for (var i = 0, length = names.length; i < length; i++) {
85
- (function(name) {
86
- self.$$proto[name] = nil;
87
- var func = function() { return this[name] };
86
+ var proto = self.$$proto;
88
87
89
- if (self.$$is_singleton) {
90
- self.$$proto.constructor.prototype['$' + name] = func;
91
- }
92
- else {
93
- Opal.defn(self, '$' + name, func);
94
- }
95
- })(names[i]);
88
+ for (var i = names.length - 1; i >= 0; i--) {
89
+ var name = names[i],
90
+ id = '$' + name;
91
+
92
+ // the closure here is needed because name will change at the next
93
+ // cycle, I wish we could use let.
94
+ var body = (function(name) {
95
+ return function() {
96
+ return this[name];
97
+ };
98
+ })(name);
99
+
100
+ // initialize the instance variable as nil
101
+ proto[name] = nil;
102
+
103
+ if (self.$$is_singleton) {
104
+ proto.constructor.prototype[id] = body;
105
+ }
106
+ else {
107
+ Opal.defn(self, id, body);
108
+ }
96
109
}
97
110
}
98
111
@@ -101,25 +114,35 @@ def attr_reader(*names)
101
114
102
115
def attr_writer ( *names )
103
116
%x{
104
- for (var i = 0, length = names.length; i < length; i++) {
105
- (function(name) {
106
- self.$$proto[name] = nil;
107
- var func = function(value) { return this[name] = value; };
117
+ var proto = self.$$proto;
108
118
109
- if (self.$$is_singleton) {
110
- self.$$proto.constructor.prototype['$' + name + '='] = func;
111
- }
112
- else {
113
- Opal.defn(self, '$' + name + '=', func);
119
+ for (var i = names.length - 1; i >= 0; i--) {
120
+ var name = names[i],
121
+ id = '$' + name + '=';
122
+
123
+ // the closure here is needed because name will change at the next
124
+ // cycle, I wish we could use let.
125
+ var body = (function(name){
126
+ return function(value) {
127
+ return this[name] = value;
114
128
}
115
- })(names[i]);
129
+ })(name);
130
+
131
+ // initialize the instance variable as nil
132
+ proto[name] = nil;
133
+
134
+ if (self.$$is_singleton) {
135
+ proto.constructor.prototype[id] = body;
136
+ }
137
+ else {
138
+ Opal.defn(self, id, body);
139
+ }
116
140
}
117
141
}
142
+
118
143
nil
119
144
end
120
145
121
- alias attr attr_accessor
122
-
123
146
def autoload ( const , path )
124
147
%x{
125
148
var autoloaders;
0 commit comments