-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Override order is reversed when providing functions instead of Objects #20
Comments
What's the case you need to pass |
@dfilatov exports.testInheritedFunctionsWithPrivateVariables = function(test) {
test.expect(2);
var A = inherit(function(){
var _privateVariable = 1;
this.method = function() {
return _privateVariable+2;
};
});
var B = inherit(A, function(){
var _privateVariable = 9;
this.method = function() {
return _privateVariable+2;
};
});
test.equal(new A().method(), 3); // Works
test.equal(new B().method(), 11); // Throws up
test.done();
}; Output: ✖ testInheritedFunctionsWithPrivateVariables
AssertionError: 3 == 11
at Object.equal (/usr/local/lib/node_modules/nodeunit/lib/types.js:83:39)
at Object.exports.testInheritedFunctionsWithPrivateVariables (/home/kafoso/git/inherit/test/test.js:145:10)
at Object.<anonymous> (/usr/local/lib/node_modules/nodeunit/lib/core.js:236:16)
at /usr/local/lib/node_modules/nodeunit/lib/core.js:236:16
at Object.exports.runTest (/usr/local/lib/node_modules/nodeunit/lib/core.js:70:9)
at /usr/local/lib/node_modules/nodeunit/lib/core.js:118:25
at /usr/local/lib/node_modules/nodeunit/deps/async.js:513:13
at iterate (/usr/local/lib/node_modules/nodeunit/deps/async.js:123:13)
at /usr/local/lib/node_modules/nodeunit/deps/async.js:134:25
at /usr/local/lib/node_modules/nodeunit/deps/async.js:515:17 |
@kafoso You still can do this with __constructor property, isn't it? exports.testInheritedFunctionsWithPrivateVariables = function(test) {
test.expect(2);
var A = inherit(function(){
var _privateVariable = 1;
this.method = function() {
return _privateVariable+2;
};
});
var B = inherit(A, { __constructor: function(){
this.__base.apply(this, arguments);
var _privateVariable = 9;
this.method = function() {
return _privateVariable+2;
};
}});
test.equal(new A().method(), 3); // Works
test.equal(new B().method(), 11); // Should also works as expected
test.done();
}; |
@dfilatov Do we want this behaviour as a sugar? |
I don't think so. I still don't understand what a sugar is. |
The variable is only available within the constructor's function scope, then. What I'm asking for is private variables, which are available in every function within the "class" Object, but inaccessible from the outside. Entirely like visibility keywords work in PHP, Java and many other high-level programming languages. |
In
test.js
,exports.testOverride
verifies that the "youngest" Object (or "class") overrides the same-name function of its parent (or "base"). This test of course works.However, if the
prop
argument is a function, the override order is reversed. See example below.The above test fails. See output below.
Naturally, the
prop
argument should be an Object per your documentation. However, it would be a nice addition.The text was updated successfully, but these errors were encountered: