-2

I want to be able to do something like

var x = {};
x.something = function(y){
   console.log(y);
};
x("hi"); // Call it without using .something

Is this possible? Any ideas?

WebGL3D
  • 454
  • 6
  • 12

2 Answers2

3
var x = function(str) {
  return x.something(str);
};

x.something = function(str) { 
  console.log(str);
};
kryger
  • 12,906
  • 8
  • 44
  • 65
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

Functions are objects, so you could do something like:

var x = function(y) {
  console.log(y);
}
x.prop = function(){ return 'This works'; };

x('hi');
console.log(x.prop());
Tibos
  • 27,507
  • 4
  • 50
  • 64