JavaScript Using a javascript constructor to re-initialize an existing object using javascript constructor function,javascript using constructor,javascript with constructor,

I'm using this function to create a particle for my particle system:

function particle()
{
    this.speed = {x: -1.5+Math.random()*3, y: -12+Math.random()*12};
    this.location = {x: 50, y: 150};
    this.radius = 5+Math.random()*8;
    this.life = 4+Math.random()*8;
    this.remaining_life = this.life;
    this.r = 255;
    this.g = 140;
    this.b = 30;
}

and I'm using this function to update the particles characteristics during the course of my animation:

particle.prototype.updateparticle = function()
{
    this.remaining_life--;
    this.radius--;
    this.location.x += this.speed.x;
    this.location.y += this.speed.y;

    if(this.remaining_life < 0 || this.radius < 0)
    {
        this.speed = {x: -1.5+Math.random()*3, y: -12+Math.random()*12};
        this.location = {x: 50, y: 150};
        this.radius = 5+Math.random()*8;
        this.life = 4+Math.random()*8;
        this.remaining_life = this.life;
        this.r = 255;
        this.g = 140;
        this.b = 30;    
    }
}

Is there a way with which I can avoid the redundant code?

Also I tried using this = new particle() and it didn't work. I can't think of a reason why it shouldn't work. why doesn't it?

And on a totally unrelated note, is Firefox incapable of handling particle animations? Chrome uses 5% of my CPU. Firefox uses around 30 and still lags! I have an i5 2500k so that shouldn't be a problem. I'm running the latest versions of both.

Answer:1

Apply the function, passing the current object as the this argument:

particle.prototype.updateparticle = function()
{
    this.remaining_life--;
    this.radius--;
    this.location.x += this.speed.x;
    this.location.y += this.speed.y;

    if(this.remaining_life < 0 || this.radius < 0)
    {
        particle.apply(this);
    }
}
Answer:2

Create a prototype function to initialize the values

function particle() {
    this.init();
}

particle.prototype.init = function(){
    this.speed = {x: -1.5+Math.random()*3, y: -12+Math.random()*12};
    this.location = {x: 50, y: 150};
    this.radius = 5+Math.random()*8;
    this.life = 4+Math.random()*8;
    this.remaining_life = this.life;
    this.r = 255;
    this.g = 140;
    this.b = 30;
}

particle.prototype.updateparticle = function() {
    this.remaining_life--;
    this.radius--;
    this.location.x += this.speed.x;
    this.location.y += this.speed.y;

    if(this.remaining_life < 0 || this.radius < 0) {
        this.init();  
    }
}
Answer:3

You can make it another function and invoke whenever required.

function particle()
{
    this.initialize();
}

particle.prototype.initialize = function(){
    this.speed = {x: -1.5+Math.random()*3, y: -12+Math.random()*12};
    this.location = {x: 50, y: 150};
    this.radius = 5+Math.random()*8;
    this.life = 4+Math.random()*8;
    this.remaining_life = this.life;
    this.r = 255;
    this.g = 140;
    this.b = 30;    
}

particle.prototype.updateparticle = function()
{
    this.remaining_life--;
    this.radius--;
    this.location.x += this.speed.x;
    this.location.y += this.speed.y;

    if(this.remaining_life < 0 || this.radius < 0)
    {
        this.initialize();
    }
}
Answer:4

I have the following table that pull out from database within loop. I would like to drag and drop the following rows. The rowspan of "Sort" button column is dynamic. (It will be the count of rows). ...

I have the following table that pull out from database within loop. I would like to drag and drop the following rows. The rowspan of "Sort" button column is dynamic. (It will be the count of rows). ...

  1. donald trump
  2. donovan mitchell
  3. donald trump twitter
  4. don toliver
  5. donuts
  6. donald trump age
  7. donatos
  8. donald glover
  9. donald trump net worth
  10. donuts near me
  11. donald trump jr
  12. donepezil
  13. donald trump speech today
  14. don johnson
  15. donna summer
  16. donald cerrone
  17. don julio
  18. donald mcneil
  19. donnie darko
  20. don quixote

I am new to Javascript programming and I am trying to validate a date entered into an <input> from a calender snippet which is obtained from an external Javascript file. I am trying to validate ...

I am new to Javascript programming and I am trying to validate a date entered into an <input> from a calender snippet which is obtained from an external Javascript file. I am trying to validate ...

  1. javascript validate date format
  2. javascript validate date string
  3. javascript validate date format mm/dd/yyyy
  4. javascript validate date object
  5. javascript validate date range
  6. javascript validate date input
  7. javascript validate date format yyyy-mm-dd
  8. javascript validate date format dd/mm/yyyy
  9. javascript validate date format dd-mmm-yyyy
  10. javascript validate date of birth
  11. javascript validate date format dd/mm/yy
  12. javascript validate date greater than today
  13. javascript validate date field
  14. javascript validate date in future
  15. javascript validate date time string
  16. javascript validate date time
  17. javascript validate date string regex
  18. javascript validate date from to
  19. javascript validate date regex
  20. javascript validate date less than today

There are 3 sides, which I'm using Ajax to avoid having to reload the page to refresh. This 3 side content, and js : Content: <ul id="nav"> <li><a href="ajax_adat.php?id=8&epul=0"&...

There are 3 sides, which I'm using Ajax to avoid having to reload the page to refresh. This 3 side content, and js : Content: <ul id="nav"> <li><a href="ajax_adat.php?id=8&epul=0"&...

  1. ajax refresh page on success
  2. ajax refresh page without reloading
  3. ajax refresh page every 10 seconds
  4. ajax refresh page after submit
  5. ajax refresh page on button click
  6. ajax refresh page automatically
  7. ajax refresh page every 5 minutes
  8. ajax refresh page
  9. ajax refresh page after post
  10. ajax refresh page timer
  11. ajax refresh page php
  12. ajax refresh page without blink
  13. ajax refresh page on modal close
  14. ajax refresh page content
  15. ajax refresh page jsf
  16. ajax refresh page every seconds
  17. ajax refresh page after time
  18. ajax refresh page asp.net
  19. ajax refresh page onclick
  20. ajax refresh page interval

In my Asp.net MVC 5 application i currently have three modules and each module have it's own script files and style sheet files and there are some common scripts and style sheet files also, these are ...

In my Asp.net MVC 5 application i currently have three modules and each module have it's own script files and style sheet files and there are some common scripts and style sheet files also, these are ...

  1. dynamically set java_home
  2. dynamically set java property
  3. dynamically change combobox java
  4. dynamically change classpath java
  5. dynamically change array java
  6. dynamically change array size java
  7. dynamically change variable name java
  8. dynamically change log level java
  9. java dynamically change annotation value
  10. java dynamically change return type
  11. java dynamically change jlabel text
  12. java dynamically change array length
  13. java dynamically change method
  14. java dynamically change heap size
  15. java dynamically change variable type