How javascript appends content to an existing function

how does javascript append content to an existing function
Apr.01,2021

if the original function cannot be modified, it will be very difficult


the title does not clearly describe what to add.
first, if you want to append attributes or methods, just add them directly. After all, Funciton is also an Object type
second. If you want to maintain the existing function function and enhance the custom function code, you can rewrite the function

.
if(fnName){
    var oldFn = fnName;
    fnName = function (){
        //do yourself something
        olfFn()
        //do yourself something
    }
}

I don't know the subject. Do you want to ask this:

    function test(a,b,c = ''){
        if(c !== ''){
            // c c 
            // 
            c(); 
        }
        // do something
    }
    test.add = function (){
       console.log('do something2')
    }

function test(){
// some code
}
var newTest = test.toString().split(''); // 
newTest.splice(newTest.length-1, 0 ,'// new code'); //
newTest.push('test();'); //
newTest = new Function(newTest.join('')); // 

javascript Decoration pattern?
add other functions to the function without modifying the original function.

Menu