LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-20-2014, 12:06 AM   #1
cin_
Member
 
Registered: Dec 2010
Posts: 277

Rep: Reputation: 24
Javascript :: Objects :: Mutability and Infinity


:: mutability::

i am having trouble getting javascript to do what i want it to do..
especially in regard to objects and my understanding of their apparent mutability

Code:
# nodejs
>ae={}
{}
>ea=ae
{}
>ae.in=17
17
>ea
{ in: 17 }
so this speaks to me as a mutable object, as i understand it: since ea=ae if ae is changed, even after the declaration of ea, ea also changes

i want to be able to store a value of an object in another object and then manipulate the object allowing the store to retain the original value for later comparison

Code:
# nodejs 
> ae={in:17}
{ in: 17 }
> oleae=ae
{ in: 17 }
> store={ae:{},ole:oleae}
{ ae: {}, ole: { in: 17 } }
> ae.in=34
34
> store.ae=ae
{ in: 34 }
> store
{ ae: { in: 34 }, ole: { in: 34 } }
this is a failed attempt because the old value mutated with the new

i figured a way around it, but it seems labor intensive:
Code:
# nodejs 
> ae={in:17}
{ in: 17 }
> oleae=JSON.stringify(ae)
'{"in":17}'
> olenu=JSON.parse(oleae)
{ in: 17 }
> store={ae:{},ole:olenu}
{ ae: {}, ole: { in: 17 } }
> ae.in=34
34
> store
{ ae: {}, ole: { in: 17 } }
> store.ae=ae
{ in: 34 }
> store
{ ae: { in: 34 }, ole: { in: 17 } }
it seems to me the type change, or function call, breaks the chain of reference..?

is there a better way?


.
.
.

i have looked into Object.freeze()[(i)], but .freeze() has its own behaviors that i have difficulty understanding

for instance:
Code:
# nodejs
> freezd={ "brr":{"cool":"cold"} }
{ brr: { cool: 'cold' } }
> Object.freeze(freezd)
> freeze.brr.nu=17
17
> freezd
{ brr: { cool: 'cold', nu: 17 } }
> freezd.nu=17
undefined
> freezd
{ brr: { cool: 'cold', nu: 17 } }
the spec calls this 'shallow', i guess meaning freeze refuses to freeze recursively down an object's properties..

and worse:
Code:
# nodejs 
> ae={"in":17}
{ in: 17 }
> oleae=ae
{ in: 17 }
> Object.freeze(oleae)
{ in: 17 }
> ae.nu=34
undefined
> ae.in=34
undefined
> ae
{ in: 17 }
> oleae
{ in: 17 }
here the freeze follows the abstracted object

if i can do what i did with JSON.stringify() and JSON.parse() using Object.freeze() i have yet to figure out how..?


(i) https://developer.mozilla.org/en-US/.../Object/freeze

if you are more comfortable using your browser than node..

here is a fiddle:
http://jsfiddle.net/Uar49/

and the source:
jsplay.htm ::
Code:
<html>
<body>
  <p id="out">ae</p>
</body>
<script src="jsplay.js"></script>
</html>
jsplay.js ::
Code:
//onit

"use strict";
console.log("FIRST TEST ________________")

var ae={}
console.log(ae) // {}
var ea=ae
console.log(ea) // {}
ae.in=17
console.log(ae.in) // 17
console.log(ea.in) // 17


console.log("SECOND TEST ________________")

var secae={in:17}
console.log(secae) // { in: 17 }
var secoleae=secae
console.log(secoleae) // { in: 17 }
var secstore={"secae":{},"secoleae":secoleae}
console.log(secstore) // { ae: {}, ole: { in: 17 } }
secae.in=34
secstore.secae=secae
console.log(secstore) // { secae: { in: 34 }, secoleae: { in: 34 } }


  
console.log("THIRD TEST ________________")
//this test works as i would like it to

var thiae={in:17}
console.log(thiae) // { in: 17 }
var thioleae=JSON.stringify(thiae)
console.log(thioleae) // '{"in":17}'
var thiolenu=JSON.parse(thioleae)
console.log(thiolenu) // { in: 17 }
var thistore={"thiae":{},"thioleae":thiolenu}
console.log(thistore) // { ae: {}, ole: { in: 17 } }
thiae.in=34
thistore.thiae=thiae
console.log(thistore) // { ae: { in: 34 }, ole: { in: 17 } }

console.log("FOURTH TEST ________________")

var freezd={ "brr":{"cool":"cold"} }
console.log(freezd) // { brr: { cool: 'cold', nu: 17 } } // frustrating that the console shows the final state stead the state as the code cascades
var pout=document.getElementById("out")
pout.innerHTML=JSON.stringify(freezd) // {"brr":{"cool":"cold"}} //cascaded value
//this is frustrating because it shows the 
Object.freeze(freezd)
freezd.brr.nu=17
console.log(freezd) // { brr: { cool: 'cold', nu: 17 } }
//freezd.nu=17 // Uncaught TypeError: Can't add property nu, object is not extensible 



console.log("FIFTH TEST ________________")
var fifae={"in":17}
console.log(fifae) // { in: 17 }
var fifoleae=fifae
console.log(fifoleae) // { in: 17 }
Object.freeze(fifoleae)
//fifae.nu=34 // Uncaught TypeError: Can't add property nu, object is not extensible 
//fifae.in=34 // Uncaught TypeError: Can't add property nu, object is not extensible 
console.log(fifae) // { in: 17 }

console.log("SIXTH TEST ________________")
var sixae={"in":17}
console.log(sixae) // { in: 17 }
var sixoleae=sixae
console.log(sixoleae) // { in: 17 }

//var store={"sixae":{},"sixoleae":sixoleae}
//console.log(store)
//Object.freeze(sixoleae)
//sixae.nu=17 // Uncaught TypeError: Can't add property nu, object is not extensible 

var store={"sixae":sixae,"sixoleae":sixoleae}
console.log(store)
Object.freeze(sixoleae)
//store.sixae.nu=17 // Uncaught TypeError: Can't add property nu, object is not extensible 7
:: infinity ::

also, in my efforts i came across this odd behaviour:

Code:
//place in js file
console.log("Infinity..________________")  
var infinite={ae:{}, "down": {"turtle": "all the way" }}
infinite.ae=infinite.down
infinite.down.turtle=infinite.ae
console.log(infinite)
..from the console

Code:
Infinity..________________ jsplay.js:87
Object {ae: Object, down: Object}
  > ae: Object
  v down: Object
   v turtle: Object
     v turtle: Object
       v turtle: Object
         v turtle: Object
           > turtle: Object
              ..
this will allow me to continually click though the infinite object forever

Last edited by cin_; 07-20-2014 at 01:36 AM. Reason: gramm`err
 
Old 07-20-2014, 01:55 AM   #2
cin_
Member
 
Registered: Dec 2010
Posts: 277

Original Poster
Rep: Reputation: 24
figured

seems my method of JSON.parse() and JSON.stringify() was the way to do it:

http://stackoverflow.com/questions/1...344074#5344074
Quote:
In my previous tests where speed was a main concern I found JSON.parse(JSON.stringify(obj)) to be the fastest way to Deep clone an object (it beats out JQuery.extend with deep flag set true by 10-20%).

JQuery.extend is pretty fast when deep flag is set to false (shallow clone). It is a good option because it includes some extra logic for type validation and doesnt copy over undefined properties, etc. but this will also slow you down a little.

If you know the structure of the objects you are trying to clone or can avoid deep nested arrays you can write a simple for (var i in obj) loop to clone your object while checking hasOwnProperty and it will be much much faster than JQuery.

Lastly if you are attempting to clone a known object structure in a hot loop you can get MUCH MUCH MORE PERFORMANCE by simply in-lining the clone procedure and manually constructing the object. JS trace engines suck at optimizing for..in loops and checking hasOwnProperty will slow you down as well. Manual clone when speed is an absolute must.
manual is impossible in my usecase, and i'm library free at the moment and like to stay that way, so it seems JSON.parse(JSON.stringify(obj)) is the way to go

Last edited by cin_; 07-20-2014 at 01:55 AM. Reason: gramm`err
 
  


Reply

Tags
immutable, javascript, mutability, object


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ubuntu and infinity tatty head Linux - Networking 1 06-30-2013 01:16 PM
Global Objects in Javascript- Possible to Exploit? dman777 Programming 5 07-13-2012 09:17 AM
LXer: Java Data Objects and Service Data Objects in SOA LXer Syndicated Linux News 0 01-17-2009 06:10 AM
javascript > embeded objects > onMediaComplete() not working on OSX BrianK Programming 0 07-02-2008 09:48 PM
dynamically add objects in JavaScript djgerbavore Programming 1 03-11-2005 09:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration