Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Billy B.
    @Azurasky1
    that is good to know
    thank you
    Andrea Sonny
    @andreasonny83
    var obj = {};
    var obj2 = obj;
    obj2.player = ‘myPlayer’;
    
    console.log(obj); // {player: ‘myPlayer’}
    objects are always copied by reference in javascript
    you can try by yourself
    try to really clone an object in javascript. is really tricky
    Billy B.
    @Azurasky1
    i think that is one of the topics that was covered in the udemy course
    by reference v.s. by value
    Andrea Sonny
    @andreasonny83
    yes
    Billy B.
    @Azurasky1
    goes to show how much i remembered... lol
    i was thinking of going through it again and taking notes
    Andrea Sonny
    @andreasonny83
    but if you really want to create a copy of that object, is really not easy in javascript
    this is what you need to write if you really want to clone an object to store in a different variable:
    function cloneObject(obj) {
        var clone = {};
        for(var i in obj) {
            if(typeof(obj[i])=="object" && obj[i] != null)
                clone[i] = cloneObject(obj[i]);
            else
                clone[i] = obj[i];
        }
        return clone;
    }
    Billy B.
    @Azurasky1
    jeez lol
    Andrea Sonny
    @andreasonny83
    not really 1 line of code
    Billy B.
    @Azurasky1
    so all i have to do to edit an object from another file is pass it in as a parameter to a function and create a local variable
    Andrea Sonny
    @andreasonny83
    yes
    that is just passing over the address in where the object is stored
    but, if you pass a variable instead of an object, then you will create a new variable
    Billy B.
    @Azurasky1
    does this count as an object
    var bodies = [];
    Andrea Sonny
    @andreasonny83
    var a = 10;
    var b = a;
    b = 20;
    
    console.log(a); // 10
    yes, arrays are special kind of objects
    Billy B.
    @Azurasky1
    so it's by reference too?
    Andrea Sonny
    @andreasonny83
    yes
    Billy B.
    @Azurasky1
    ok
    Andrea Sonny
    @andreasonny83
    you can create a javascript sandbox in where you can run simple tests
    Billy B.
    @Azurasky1
    i guess i might as well include bodies [] within game {}
    Andrea Sonny
    @andreasonny83
    just create a new sandbox.js file and then you can run from your terminal with: node sandbox.js
    Billy B.
    @Azurasky1
    awesome
    i'll do that
    Andrea Sonny
    @andreasonny83
    write this code inside:
    var myVar = [];
    var _var = myVar;
    
    _var.push({
      player: {
        name: 'Joe'
      }
    });
    
    
    console.log(myVar);
    myVar will print what you put inside _var
    so, if you want to push objects inside your array, just use push and pass the object inside the brackets
    Billy B.
    @Azurasky1
    what is this
      modules.Projectiles = (function() {
        return {
          projectiles: [],
          new: function(cv, x, y, width, height, direction, speed) {
            this.projectiles.push(
              new Projectile(cv, x, y, width, height, direction, speed));
          },
    
          getActive: function() {
            return this.projectiles.length;
          },
    
          draw: function() {
            for (var i = 0; i < this.projectiles.length; i++) {
              this.projectiles[i].draw();
            }
          }
        };
    Andrea Sonny
    @andreasonny83
    is the projectile service
    Billy B.
    @Azurasky1
    i have never seen an iife used in that way
    var batman = (function () {
        var identity = "Bruce Wayne";
    
        return {
            fightCrime: function () {
                console.log("Cleaning up Gotham.");
            },
    
            goCivilian: function () {
                console.log("Attend social events as " + identity);
            }
        };
    })();
    Billy B.
    @Azurasky1
    so what data are we trying to keep private?
    Andrea Sonny
    @andreasonny83
    we return eveything
    Billy B.
    @Azurasky1
    is the information before "return" the private information? or is it just everything within the iife?
    that's what i thought
    Andrea Sonny
    @andreasonny83
    that is optional
    but we have the projectiles array in where we store all the projectiles active in the game
    and the new method just push a new projectile object inside that array
    in this way we don’t have a draw method inside each projectile
    we just store an array of projectiles ready to be drawed inside the canvas
    Billy B.
    @Azurasky1
    if we want to hide the array so no one can write to it shouldn't we move it to the line above return