Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6943943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:13:18+00:00 2026-05-27T13:13:18+00:00

So, i made this code to help me add functions after or before other

  • 0

So, i made this code to help me add functions after or before other functions, but i couldn’t figure out a better way to do that, i had to use eval() and that is really not a good practice. At first, i tried to do something like:

Function.prototype.append = function(fn){
    eval("this = function(){ ("+this.toString()+").apply(this, arguments); fn.apply(this, arguments); }");
}

hello = function(){
    console.log("hello world!");
}

hello(); // hello world!
hello.append(function(){
    console.log("bye world!");
});
hello(); // hello world! bye world

but it didn’t work since the function can’t change itself. So i did this:

Aspects = new Object();

Aspects.append = function(aspect, fn){
    eval(aspect + " = function(){ ("+eval(aspect + '.toString()')+").apply(this, arguments); fn.apply(this, arguments); }");
}

Aspects.prepend = function(aspect, fn){
    eval(aspect + " = function(){ fn.apply(this, arguments); ("+eval(aspect + '.toString()')+").apply(this, arguments); }");
}

hello = function(){
    console.log("hello world!");
}

hello(); // hello world!

Aspects.append('hello', function(){
    console.log("bye world!");
});

hello(); // hello world! bye world!

i don’t want to work with objects or anything, i just want to add more code after or before my already declared function

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:13:19+00:00Added an answer on May 27, 2026 at 1:13 pm

    I have a utility library called fcombine which supports

    f1 = fcombine.pre(new_function, f1);
    f1 = fcombine.post(f1, new_function);
    

    The reason your code is ugly is because your not returning the new function.

    The problem with your code is also that it uses eval and tries to do too much magic. The whole idea of passing in a string of a variable name then evalling that is absolutely horrible.

    You can quite easily write

    hello = Aspects.append(hello, new_function);

    As demonstrated by fcombine.post

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made this code with jQuery to fade images ( but not the one
I am pretty new at C# and .NET, but I've made this code to
Ok. I've made this awe-crap-code Exporting table to XLS (but still no idea how
i made a windows service & add project installer.in which only contain this code.
Hey. I know this is not a 'refactor my code' site but I made
How can I improve this code? What has made this long winded is the
I have roughly the following code. Could this be made nicer or more efficient?
I've used this code in another site I've made and it works absolutely fine
Hi, I have made this simple script but I need to have it check
I made this bookmarklet: javascript:(function(){var s=document.createElement('script');s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');document.getElementsByTagName('body')[0].appendChild(s);$('#hldIntMain').hide();$('#fadeBackground').hide();return false;})() Formatted code: // Add in jQuery var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.