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

  • SEARCH
  • Home
  • 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 8388577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:30:00+00:00 2026-06-09T18:30:00+00:00

I was searching up how to fade an element with JavaScript earlier and I

  • 0

I was searching up how to fade an element with JavaScript earlier and I came across this function (object). I began wondering how does it work?

var fadeEffect=function(){
return{
    init:function(id, flag, target){
        this.elem = document.getElementById(id);
        clearInterval(this.elem.si);
        this.target = target ? target : flag ? 100 : 0;
        this.flag = flag || -1;
        this.alpha = this.elem.style.opacity ? parseFloat(this.elem.style.opacity) * 100 : 0;
        this.si = setInterval(function(){fadeEffect.tween()}, 20);
    },
    tween:function(){
        if(this.alpha == this.target){
            clearInterval(this.elem.si);
        }else{
            var value = Math.round(this.alpha + ((this.target - this.alpha) * .05)) + (1 * this.flag);
            this.elem.style.opacity = value / 100;
            this.elem.style.filter = 'alpha(opacity=' + value + ')';
            this.alpha = value
        }
    }
}
}();

I know that this is self invoking and only returns one object with two methods. My main concern this why does it use the this keyword? I am assuming the ‘this’ keyword is a placeholder for the object name “fadeEffect”. I would understand if ‘this’ was used to create multiple objects… but why is it used here?

One other thing bothering me is this ternary operator…

   this.target = target ? target : flag ? 100 : 0;

How the heck does that work? It’s like two ternary operators combined into one which I never thought was possible?

  • 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-06-09T18:30:02+00:00Added an answer on June 9, 2026 at 6:30 pm

    Think of it as a namespace. the this keyword refers back to the object literal that the self invoking function returns. This means that this.target is accessible in the global namespace (or whatever scope the fadeEffect was defined) as an object property: fadeEffect.target, but it doesn’t interfere with other variables that may exist in the outer scope.

    The two methods set new properties of the returned object, that’s all there is to it. Personally I find this to be, well, bad code… a closure would have been the better choice in this example:

    var fadeEffect=function(){
        var elem,target,flag,alpha,si;//make private
        return{
            init:function(id, flag, target){
                elem = document.getElementById(id);
                clearInterval(elem.si);
                target = target ? target : flag ? 100 : 0;
                flag = flag || -1;
                alpha = elem.style.opacity ? parseFloat(elem.style.opacity) * 100 :0;
                si = setInterval(function(){fadeEffect.tween()}, 20);
            },
            tween:function(){
                if(alpha == target){
                    clearInterval(si);//this.elem.si doesn't add up, init defines it as this.si
                }else{
                    var value = Math.round(alpha + ((target - alpha) * .05))+ (1 * flag);
                    elem.style.opacity = value / 100;
                    elem.style.filter = 'alpha(opacity=' + value + ')';
                    alpha = value
                }
            }
        }
    }();
    

    This does the same thing, but other code cannot interfere with the values of the target, or mess up the interval etc… you’re right to say that the this keyword isn’t required in this case, but I think the person who wrote this was either unfamiliar with JS closures, or at least insecure about how they work. This code effectively simulates a singleton pattern, or at least treats the object literal as an instance of a class. My guess is, the author is familiar with classical OOP, but not with prototypal inheritance. Anyway, the above code is safer, and safer is better IMHO


    On the matter of your nested ternary, I’ve checked the code below using JSLint, and it suggested an even shorter, yet clearer alternative: use the default operator, followed by a ternary:

     //JSLint recommends this
     target = argTarget || argFlag ? 100 : 0;
     //over nested ternary
     target = argTarget ? argTarget : argFlag ? 100 : 0;
    

    Anyway, here’s the same code, only not using the dangerous this constructs, but using a closure, one of JavaScripts amazingly powerful features BTW, worth taking a closer look at what you can do with them!

    var fadeEffect=(function()
    {
        var elem,target,flag,alpha,si;//make private
        //define private 'methods': functions will be available, but only to return object
        //tween shouldn't be callable, it's a callback for the interval, which is set in init
        function tween()
        {
            if(alpha === target)
            {
               clearInterval(si);//this.elem.si doesn't add up, init defines it as this.si
            }
            else
            {
                alpha = Math.round(alpha + ((target - alpha) * 0.05))+ (1 * flag);
                //don't know why 1*flag is needed here, suggest:
                //alpha = Math.round(alpha + ((target - alpha) * 0.05)) + (+flag); +flag coerces to numeric
                elem.style.opacity = alpha / 100;
                elem.style.filter = 'alpha(opacity=' + alpha + ')';
            }
        }
        return{
            init:function(id, argFlag, argTarget)//arguments !== closure scope
            {
                if (si !== undefined && si !== null)
                {
                    clearInterval(si);
                }
                elem = document.getElementById(id);
                //JSLint recommends this:
                target = argTarget || argFlag ? 100 : 0;
                //over nested ternary
                target = argTarget ? argTarget : argFlag ? 100 : 0;
                flag = argFlag || -1;
                alpha = elem.style.opacity ? parseFloat(elem.style.opacity) * 100 :0;
                si = setInterval(tween, 20);//just a reference to the tween function will do
            }
        };
    })();
    fadeEffect.init('someId',1,50);//will set things in motion
    fadeEffect.tween();//undefined
    console.log(fadeEffect.target);
    fadeEffect.target = document.getElementById('someOtherId');//no problem, but won't change the value of var target
    

    This way, the tween method cannot be called but by the interval, and the element on which the object, and its methods/functions are working their magic can never be overridden by external operations, they are inherent to the object. This makes for a safer construction, what’s more, you can only really mess up 1 method: override the .init method, and the object is rendered useless, but harmless. Compare that to your code, where you could mess up both methods, but leave the interval standing… that’s bad news: the interval would end up looking for a callback function that could very well have been deleted, causing your code to fail miserably:

    //asume your code using this.tween();
    fadeEffect.init('id',1,123);
    delete fadeEffect.tween;
    //inside fadeEffect:
    setInterval(function(){fadeEffect.tween()}, 20);
    //should be written as:
    setInterval(fadeEffect.tween,20);
      // === setInterval(undefined,20); === :-(
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Searching through gluon.validators I came across IS_LIST_OF() so I tried to add it to
While searching for difference between HTML4 and HTML5 I came across the point that
Searching something on SO, I stumbled across this question and one of the comments
Searching around I haven't found any recent answers to this question. The other answers
Searching here I found that this question was already asked , but I think
Searching for a simple first-child detection via javascript (no framework). It should add class
Searching for a way to add this code, after the <head> (or some <link
I would like to learn how to do fade, and similar effects on JavaScript.
I've been searching everywhere for this, hope some of you experts can help me
Searching the web for some method to send POST requests in Objective-C, I came

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.