I know javascript prototype inheritance pretty well but I have a situation similar to angular scopes
and I don't know how to handle it.
Let's begin with wrong (because it is bad idea to change __proto__
property) but working example
https://jsfiddle.net/hxm61r7j/
function Scope() {
}
Scope.prototype.$new = function() {
var new_scope = new Scope();
//TODO: change line bellow
new_scope.__proto__ = this;
return new_scope;
}
// Create root scope
var scope_level_1 = new Scope();
scope_level_1.a = 'scope_level_1 a';
scope_level_1.b = 'scope_level_1 b';
// Create scope which inherits from parent
var scope_level_2 = scope_level_1.$new();
scope_level_2.b = 'scope_level_2 b';
// We don't have property "a" in "scope_level_2" so it will be taken from "scope_level_1"
// But we have property "b" in "scope_level_2" so it will be taken from there
console.log(scope_level_2.a, scope_level_2.b);
As you see I need a way to create some kind of scope
which will have some properties. Later I need to create other scope
which will inherit from previous one. I mean if property is not defined in current scope it will be taken from parent one.
But I'm not sure how to change this line: new_scope.__proto__ = this;