Skip to content

Instantly share code, notes, and snippets.

@gartz
Last active September 16, 2015 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gartz/d33f508d32b10573f28b to your computer and use it in GitHub Desktop.
Save gartz/d33f508d32b10573f28b to your computer and use it in GitHub Desktop.
// Create a function, that receive an Function Constructor and a Object and returns a instance of the Function Constructor
// where it prototype is exposed with the property `parent` and should be the Object in the seccond param, all the other
// params should be used in the Function Constructor.
function extend(FunctionConstructor, objectInstance) {
// This is unfinished, as example to start thinking on how to do it. The prototype and arguments are missing here...
var instance = new FunctionConstructor(){};
instance.parent = objectInstance;
return instance;
}
function A(name){
this.name = name;
// Create also a method that override `hello` and uses the method from C acessing by the reference contained in the parent
// property, that will be added by the extend function
this.hello = function(){ return /* ... */};
return this;
};
function B(greeting){
this.getGreeting = function(){ return greeting; };
return this;
}
function C(){
this.getGreeting = function(){ return ''; };
this.hello = function(){ return this.getGreeting() + this.name; };
return this;
}
// When create a instance of A, it needs to have B and C as it own prototype, and have a fixed value 'hello' for the greeting
// defined by your extend function, example:
var c = new C();
var b = extend(B, c, 'hello');
var a = extend(A, b, 'Person Name');
a.hello(); // hello Person Name, welcome!
var a1 = extend(A, b, 'Another Person');
a1.hello(); // hello Another Person, welcome!
/*
*/
// Create a function, that receive an Function Constructor and a Object and returns a instance of the Function Constructor
// where it prototype is exposed with the property `parent` and should be the Object in the seccond param, all the other
// params should be used in the Function Constructor.
function extend(FunctionConstructor, objectInstance) {
// This is unfinished, as example to start thinking on how to do it. The prototype and arguments are missing here...
FunctionConstructor.prototype = objectInstance;
var instance = new FunctionConstructor();
instance.parent = objectInstance;
var args = Array.prototype.slice.call(arguments, 2);
return FunctionConstructor.apply(instance, args);;
}
function A(name){
this.name = name;
// Create also a method that override `hello` and uses the method from C acessing by the reference contained in the parent
// property, that will be added by the extend function
this.hello = function(){ return this.parent.hello.apply(this, arguments) + ', welcome!'; };
return this;
};
function B(greeting){
this.getGreeting = function(){ return greeting; };
return this;
}
function C(){
this.getGreeting = function(){ return ''; };
this.hello = function(){
return this.getGreeting() + this.name;
};
return this;
}
// When create a instance of A, it needs to have B and C as it own prototype, and have a fixed value 'hello' for the greeting
// defined by your extend function, example:
var c = new C();
var b = extend(B, c, 'hello ');
var a = extend(A, b, 'Person Name');
a.hello(); // hello Person Name, welcome!
var a1 = extend(A, b, 'Another Person');
a1.hello(); // hello Another Person, welcome!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment