mercredi 31 décembre 2014

Function that can return a value and also act as an object


This question seems hard to form so I will just provide an example of what I am trying to accomplish::



function test(word) {
return word
}

function replaceWord(word) {
return word
}
test('Hello').replaceWord('World')


So I want to be able to return "Hello" and if I want to, chain the replaceWord function.


Comparable to how jquery handles things like



$('#element').something('doit').somethingElse();


I feel like I am on the right track with



var test = function (word) {
return word;
}
test.prototype = {
replaceWord: function (word) {
return word;
}
}


But since I am returning the string, the chain will not work. What is the proper way I am supposed to doing this to be be able to have a function that can return a value [test('Hello')] and also can act as an object to modify data for value/s returned by the function [test('Hello').replaceWord('World')]?





Aucun commentaire:

Enregistrer un commentaire