We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

1man on 03/20/07


Tagged

javascript object link delete


Versions (?)


Linking Objects & Deleting Info in Objects


Published in: JavaScript 


Shows you how to link 2 objects together via the object function. If a value isn't found in the object, it will look for it in the linked object.

  1. //used below in "secret" linking
  2. function object(o){//new function, argument o
  3. function F() {}//define function F(empty)
  4. F.prototype = o;//add the custom property/method passed through o
  5. return new F();//return the new function with the added property/method
  6. }
  7. //Old object
  8. var myObject = {
  9. name: "Matt",
  10. dob: "12/12/1913",
  11. age: "95",
  12. ocupation: "Web Developer"
  13. }
  14. //Secret link new object to old object
  15. var myNewObject = object(myObject);
  16.  
  17. //add values to the new object
  18. myNewObject.name = "new name";
  19. myNewObject.crime = "crime";
  20.  
  21. //it can't find "age" in new object, it looks at the old object via the link.
  22. console.info(myNewObject.crime);//firebug
  23. console.info(myNewObject.age);//firebug
  24.  
  25. //delete info in an object
  26. delete myObject.ocupation;
  27. console.warn(myObject.ocupation);//firebug

Report this snippet 

You need to login to post a comment.