-1

I tried this:

 var fun = function(){ console.log("booyah"} , {
  hell: function(){
    console.log("hello");
  },
  boy: ()=>{
    alert("blah");
  }
};
fun.boy();

I want that first I call fun function and then Access functions I have stored as objects. But I am getting errors . How can I fix it? Or is there any other way to do that? Please help.

maziyank
  • 581
  • 2
  • 10
CodeMania
  • 3
  • 6
  • Note: I wanted to work like, when I call myObject.fun() where myObject acts like function and I can access the functions stored inside curly braces as objects i.e. first it act like function and by putting a . I can access functions stored inside it. Any idea? – CodeMania Mar 01 '21 at 06:16

2 Answers2

-1

Check the below code, reference taken from Can you create functions with custom prototypes in JavaScript?

function MyFun() {
    if (!this || this==window) {
        return new MyFun();
    }

    var f = function() {
        return "thanks for calling!";
    }
    f.__proto__ = MyFun.prototype;
    f.constructor = MyFun;

    return f;
}

MyFun.prototype = {
    foo: function() {
        return "foo:" + this();
    },
    __proto__: Function.prototype
};

var f = new MyFun();
alert("proto method:"+f.foo()); // try our prototype methods
alert("function method:"+f.call()); // try standard function methods
alert("function call:"+f()); // try use as a function
alert('typeof:' + typeof f); // "function", not "object". No way around it in current js versions
alert('is MyFun:' + (f instanceof MyFun)); // true
alert('is Function:' + (f instanceof Function)); // true
shotgun02
  • 756
  • 4
  • 14
  • But I wanted to work like, when I call myObject.fun() where myObject acts like function and I can access the functions stored inside curly braces as objects i.e. first it act like function and by putting a ```.``` I can access functions stored inside it. Any idea? – CodeMania Mar 01 '21 at 06:04
  • why havn't you put this explanantion in question itself ? – shotgun02 Mar 01 '21 at 06:07
  • I'm sorry for that . I think I didn't explained my question well . But I can't edit question because often community members closed my questions . I am beginner so I need time for that. Hope you understand. – CodeMania Mar 01 '21 at 06:20
  • Updated the answer, I think this answer may help to resolve your issue. If you found this helpful a upvote will make my day. :) – shotgun02 Mar 01 '21 at 06:22
  • I can't upvote you because I don't have reputations :( but I can mark as correct answer :) . – CodeMania Mar 01 '21 at 06:30
  • I wanted this to be run ```console.log(f().foo());``` but I got error ```Uncaught TypeError: f(...).foo is not a function```. I want that above code to be run properly. Any Idea. – CodeMania Mar 02 '21 at 15:00
-1

You can achieve this kind of result by editing the prototype of the function.

Example:

function foo() {
        const something = 'the thing';
    
        return something;
    }
    
    const customPrototype = {
        greeting: (name) => {
            const greetingTemplate = `Hello ${name}`;
    
            return greetingTemplate;
        },
    
        randomNumber: () => {
            return Math.random();
        },
    };
    
    // Extending prototype to access our custom prototype functions
    Object.assign(foo, customPrototype);
    
    
    console.log(foo()); // the thing
    console.log(foo.greeting('People')); // Hello People
    console.log(foo.randomNumber()); // 0.26138311987993545
    // Default prototype functions are working as well
    console.log(foo.toString()); // [object Function]

EDIT: Thanks to @Bergi for correcting me!

fayzzzm
  • 24
  • 3
  • You got it bro !! Same thing I was wanted . Can you update your answer so that I can take value of the parameter of foo function ```foo(value)``` and still the function works like ```foo.greeting("Thank you bro for helping me")``` – CodeMania Mar 01 '21 at 14:03
  • You can provide any amount of variables and write some logic inside your function. If you want to add some more functions, just update customPrototype (add you extra properties / functions theere). Feel free to write anything at there. – fayzzzm Mar 01 '21 at 19:33
  • No, don't use the deprecated `__proto__` accessor! There's no reason to mess around with prototypes here (and worse, making `foo` no longer an `instanceof Function`). Just write `Object.assign(foo, customMethods)` and you're done. – Bergi Mar 01 '21 at 19:54
  • @Bergi i've edited the solution. Thanks for help! – fayzzzm Mar 01 '21 at 20:57