« first day (4935 days earlier)      last day (13 days later) » 

5:59 AM
@matt There is a problem with this specific object you show - each of the methods will exist for each of the instances created. This can create memory bloat if you instantiate a lot of them. A class (and more properly: prototype inheritance) will reduce the overhead since all instances will share a single copy of the methods.
With that said, you are correct that the approach of creating objects in that fashion is viable. You just have to be aware of the memory problems. A solution is a trivial as "don't have methods" - you can just expose functions that consume the objects, thus you're only constructing data objects.
 
 
5 hours later…
11:06 AM
Hello everyone
express-session - Note Session data is not saved in the cookie itself, just the session ID. Session data is stored server-side.
Does it mean if we want to keep user logged in after page refresh (React app)... it's not possible at all ?
and alternative is to use cookie-session ?
 
 
1 hour later…
12:22 PM
VLAZ, it doesnt have to, i was hungry and tired when writing the above code very quickly lol, and the original question was in terms of obj's and level's so it became confusing quickly
        function level1(){

          var obj   = {};

              obj.create=function(nxt){

                    nxt.method1=method1;

              }//create

              function method1(){alert(1)}

          return obj;

        }//level1

       function level2(){

          var obj   = {};

              obj1.create(obj);

          return obj;

        }//level2

        var obj1      = level1();
        var obj2a     = level2();
        var obj2b     = level2();

        alert(obj2a.method1===obj2b.method1);
its a shame we cant run code in the stackoverflow chat
the difficulty actually comes from sharing primitive values
there are numerous ways around it, even using Object.create
the primary advantage of using functions and objects is not having to reference everything with a this reference, which for me is huge
but as it happens, my preferred way of programming is separating data from methods anyway, so i try to end up with an object that just represents the data needed for a particular module to work
 
12:37 PM
why is this still not equal ? since it's sharing same property
var a = 2
var obj1 = {
  a:a
}

var obj2 = {
  a:a
}

console.log(obj1 === obj2) // false
in the mdn it says
More formally, two objects o1 and o2 are shallow copies if:

They are not the same object (o1 !== o2).
The properties of o1 and o2 have the same names in the same order.
The values of their properties are equal.
Their prototype chains are equal.
 
with triple equals you are testing to see if they are referencing the same object, in your example they are not
there is no native test for shallow copies
function shallowEqual(object1, object2) {
  const keys1 = Object.keys(object1);
  const keys2 = Object.keys(object2);

  if (keys1.length !== keys2.length) {
    return false;
  }

  for (let key of keys1) {
    if (object1[key] !== object2[key]) {
      return false;
    }
  }

  return true;
}
is one simple test, it does not explicitly test the prototype chain
 
for (let key of keys1) {
    if (object1[key] !== object2[key]) {
      return false;
    }
  }
 
@underscore Please don't post unformatted code - use the up arrow to edit your post, then hit Ctrl + K to format the code in that post. See the faq. You have 25 seconds to edit and format your message properly before it will be removed. Please separate code blocks from your actual question. Put your question in 1 message and then your code in a 2nd and format it.
 
The for loop checks the MDN "The values of their properties are equal."
 
yes it also needs to check if object2 contains keys that object1 does not
 
12:49 PM
and it will only check the root level keys.
 
root level?
 
first property level
 
it will check all enumerable properties
 
{ a: 2 }
 
so will include prototype's
it wont actually check if the prototypes are referencing the same object though
 
12:51 PM
var a = 2
var obj1 = {
  a:a,
  b: {
    c: {
      d:2
    }
  }
}

console.log(Object.keys(obj1))
if (object1[key] !== object2[key])
So this check does not cover nested objects
 
ok i agree
 
only the first level
 
the other quick way is
  JSON.stringify(object1)===JSON.stringify(object2);
 
yes
will this cheep ?
 
sorry i dont understand
 
12:55 PM
is that fast enough ?
 
is it computationally expensive to run?
yes
you probably wouldnt want to do it 10^10 times, but in javascript thats rarely the case anyway
actually there are caveats
      var a   = {
            d   : 99,
            e   : undefined
      };

      var b   = {
            d   : 99
      };

      alert(JSON.stringify(a)===JSON.stringify(b));   //true
 
:D
Shallow actually don't care about the object references
it does check the above 4 elements
 
its actually not so easy to do it properly for every edge case
 
1:12 PM
When in array function returns something it always shallow copied.
ex: Array.from()
 
the array itself is shallow copied, the array members are copied by reference
 
yes
 
there is also the spread syntax for arrays, slightly more succinct ex: [...list]
 
spread does not shallow copied nested objects
You have to use Object.assign but it does not do the shallow
it will be deep copy
 
no it'll copy by reference, not deep copy
as does Array.from
      var a   = [node1,node2];
      var b   = [...a];
      alert(a[0]===b[0]);   //  true
or
      var a   = [{},[]];
      var b   = [...a];
      alert(a[1]===b[1]);   //  true
 
1:19 PM
var obj1 = {
  a:2
}

var obj2 = {...obj1}
console.log(obj1 === obj2) // false
This is false
var node1 = 0
var node2 = 2
var a = [node1, node2];
var b = [...a];

console.log(a === b)
You need to check the whole array a with b
not the properties
 
right sorry i thought you were referring to the members being shallow copied, copied by reference or deep copied
yes all those methods create a new object or array
nb: by node, i meant var node=document.createElement('div') or something
      var a   = [{},{}];
      var b   = a;
      alert(a===b);   //  true
theres also structuredClone for copying developer.mozilla.org/en-US/docs/Web/API/structuredClone
also Object.assign will copy members by reference, not deep copy
shallow copy : new initial object/array etc, members copy by reference
deep copy : new iniital object/array, all members are completely new copies
copy by reference : all 'objects' reference the same original object
 
1:52 PM
var obj1 = {
  a: 2,
  b: {
    c: {
      d: 2
    }
  }
}

var ob2 = {
  ...obj1
}
console.log(ob2.b.c === obj1.b.c)
Spreed does the deep copy only for first level properties, rest will be shallow copied
is that true?
var obj1 = {
  a: 2,
  b: {
    c: {
      d: 2
    }
  }
}

var obj2 = {
  ...obj1
}


obj2.b.c.d = 5

console.log(obj2.b.c.d === obj1.b.c.d)
This return true
 
its returning true for primitive values
  alert(obj2.b.c===obj1.b.c);   //  false
sorry thats not true, it will return true, its copied by reference
 
2:09 PM
Yeah
It's true
spread only deep copy the first level properties
reset will do the shallow
So this is dangers.Need more understanding before work on such thing
 
do you want a deep copy
      var obj1 = {
        a: 2,
        b: {
          c: {
            d: 2
          }
        }
      }

      var obj2 = structuredClone(obj1);

      alert(obj2.b===obj1.b);   //  false
 
3:14 PM
JS Naked Day js-naked-day.org
 
 
2 hours later…
4:58 PM
||joke
 
A SQL query walks into a bar, walks up to two tables and asks...
'Can I join you?'
 
My chat permanently has ":55403620" for some reason every time I load this page
Also I find it interesting how there were 57,000,000 messages posted in this room
 
6:01 PM
Hi guys, i know when we defined a object using { a: 2 } Js allocate memory for it and if assign a variable var some = { a : 2 } it will keep the reference until hit by the GC. So i just really want to know if we return a object inside function (){ return { a : 2 } ) does JS allocate memory to that return object ?
 
6:17 PM
heres a couple of good resources on js objects
JavaScript engine fundamentals: Shapes and Inline Caches mathiasbynens.be/notes/shapes-ics
Object structure in JavaScript engines blog.frontend-almanac.com/js-object-structure
 

« first day (4935 days earlier)      last day (13 days later) »