Here's a pure JavaScript solution:
function function1() {
this.a = 'hello 2u';
this.b = 'goodbye';
return [this.a,this.b];
}
function function2(obj){
this.a = obj.function1()[0];
this.b = obj.function1()[1];
console.log(this.a + " " + this.b);
}
var image1 = document.getElementById("image1");
var image2 = document.getElementById("image2");
image1.function1 = function1;
image2.function2 = function2;
image2.function2( image1 );
See demo
Given the officially accepted answer, surely this pure-JavaScript example can be improved, as follows:
HTML:
<img src="" id="image"><br>
JAVASCRIPT:
var image = document.getElementById("image");
image.function1 = function() {
this.a = "hello 2 the world";
this.b = "goodbye";
return this;
};
image.function2 = function() {
console.log(this.a + " " + this.b);
};
image.function1().function2();
See demo
By returning this, the methods may be chained together and the properties a and b are available to the second method. If you wish to avoid method chaining, the second method could invoke the first method, as follows:
image.function2 = function() {
this.image.function1();
console.log(this.image.a + " " + this.image.b);
}();
See demo
thisonly exists in the context of the function where it's run. After that it's destroyed. Since you're using jQuery, try using data-properties on the element instead, as these will persist for the lifetime of the page.$("#image1").data("a", "hello");to set it, andalert($("#image1").data("a"));to retrieve it.$('#image') !== $('#image')