Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "dereference" an object?

Tags:

javascript

php

In JavaScript, how do you dereference an object returned by a function?

For example this:

var tmp = getTextProperties();
font   = tmp.font;
size   = tmp.size;
color  = tmp.color;
bold   = tmp.bold;
italic = tmp.italic;

PHP has the list() construct which does something similar:

list($font, $size, $color, $bold, $italic) = getTextProperties();
like image 449
Richard Avatar asked Nov 19 '25 11:11

Richard


2 Answers

With ES6, you can destructure an object as follows:

var { font, size, color, bold, italic } = getTextProperties();

See JavaScript Destructuring assignment

like image 93
markmoxx Avatar answered Nov 21 '25 01:11

markmoxx


Destructuring and derefrencing are totally different derefrence the data using Destructuring which is totally wrong play around this..

const data = [
  {
    id: 1,
    name: "Tshirtr",
    img: "add-url",
    price: 714,
    cat: "Dress"
  },
  {
    id: 11,
    name: "short",
    img: "https://m.media-amazon.com/images/I/71e04Q53xlL._AC_UY879_.jpg",
    price: 474,
    cat: "Dress"
  },
  {
    id: 2,
    name: "Timex Men's Expedition Scout ",
    img: "https://m.media-amazon.com/images/I/91WvnZ1g40L._AC_UY879_.jpg",
    price: 470,
    cat: "Sport"
  },
  {
    id: 3,
    name: "Breitling Superocean Heritage",
    img: "https://m.media-amazon.com/images/I/61hGDiWBU8L._AC_UY879_.jpg",
    price: 200,
    cat: "Luxury"
  },
  {
    id: 4,
    name: "Casio Classic Resin Strap ",
    img: "https://m.media-amazon.com/images/I/51Nk5SEBARL._AC_UY879_.jpg",
    price: 16,
    cat: "Sport"
  },
  {
    id: 5,
    name: "Garmin Venu Smartwatch ",
    img: "https://m.media-amazon.com/images/I/51kyjYuOZhL._AC_SL1000_.jpg",
    price: 74,
    cat: "Casual"
  }
];

console.log("data-46", data);
// lets destructur--
const [one, ...rest] = data;
console.log("one-49", one);

one.boy = { name: "rahul", age: 24 };

console.log("one-53", one);
console.log("data-54", data);
// here you can see data also gets changed
// destructuring doesn't means derefrence

// just used below single line of code to dereferene the data that is cloning
const cloneData = JSON.parse(JSON.stringify(data));
console.log("cloneData", cloneData);
cloneData.push({ 99: "data no more refrencing.." });
console.log("cloneData-62", cloneData);
console.log("data-62", data);
like image 26
RAHUL RAJ Avatar answered Nov 21 '25 02:11

RAHUL RAJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!