Adding custom properties to a function

后端 未结 10 775
独厮守ぢ
独厮守ぢ 2020-11-27 10:34

Searching for appropriate answer proved difficult because of the existence of many other problems related to my keywords, so I\'ll ask this here.

As we know, functio

10条回答
  •  感情败类
    2020-11-27 11:16

    Possible addition to John Slegers great answer

    Isnt it possible that after John Slegers:

    Way 2 : adding properties after defining the function

    Adding a Way 2.5

    function doSomething() {
        doSomething.prop = "Bundy";
        doSomething.doSomethingElse = function() {
            alert("Why Hello There! ;)");
    
        };
    
        let num = 3;
        while(num > 0) {
            alert(num);
            num--;  
        }
    }
    
    sayHi();
    sayHi.doSomethingElse();
    alert(doSomething.prop);
    
    var ref = doSomething;
    
    ref();
    ref.doSomethingElse();
    alert(ref.prop);
    

    Putting in both a "variable" property and a function property for completeness sake, straight in the function declaration. Thus avoiding it to be "disconnected". Left the inner default workings of the function (a simple loop) to show that it still works. No?

提交回复
热议问题