Linking Objects & Deleting Info in Objects


/ Published in: JavaScript
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.