Guest User

Untitled

a guest
Sep 12th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>lab</title>
  4. </head>
  5. <body>
  6. <table border="1"></table>
  7. <input id="lastname" type="text"/><button id="find">Click to find</button>
  8. <div id="output"></div>
  9.  
  10. <script>
  11. contactManager = {
  12. protoContact : {
  13. toString : function() {
  14. return this.first + " " + this.last + " " + this.email;
  15. }
  16. },
  17.  
  18. Contact : function() {},
  19.  
  20. contacts : [],
  21.  
  22. build : function(params) {
  23. this.Contact.prototype = this.protoContact;
  24. var newContact = new this.Contact();
  25. for (var k in params)
  26. newContact[k] = params[k];
  27. newContact.id = Math.random();
  28. this.contacts.push(newContact);
  29. },
  30.  
  31. find : function(val, field) {
  32. if (field === undefined)
  33. field = "last";
  34. for (var i=0; i<this.contacts.length; i++)
  35. if (this.contacts[i][field] == val)
  36. return this.contacts[i];
  37. }
  38.  
  39. };
  40.  
  41. test = function() {
  42. contactManager.build({ first:"Joe1", last:"Smith1", email: "joesmith1@gmail.com"});
  43. contactManager.build({ first:"Joe2", last:"Smith2", email: "joesmith2@gmail.com"});
  44. contactManager.build({ first:"Joe3", last:"Smith3", email: "joesmith3@gmail.com"});
  45. c1 = contactManager.find("Smith2");
  46. console.log(c1);
  47. document.getElementById("output").innerHTML = "Found " + c1.email;
  48. c2 = contactManager.find("Joe3", "first");
  49. console.log(c2);
  50. document.getElementById("output").innerHTML += "<br/>Found " + c2.email;
  51. }
  52.  
  53. test();
  54.  
  55. document.getElementById("find").addEventListener("click", function() {
  56. var found = contactManager.find(document.getElementById("lastname").value);
  57. document.getElementById("output").innerHTML +=
  58. "<br/>" + found.toString();
  59. var row = document.getElementsByClassName(found.id)[0];
  60. row.style.background="yellow";
  61. setTimeout(function(){row.style.background="";},1000);
  62. });
  63.  
  64. h1 = document.createElement("h1");
  65. h1.innerHTML="Contacts";
  66. document.body.insertBefore(h1, document.querySelector("input"));
  67.  
  68. render = function(contacts) {
  69. createCell = function(text, parent) {
  70. var tdCell = document.createElement("td");
  71. tdCell.innerHTML = text;
  72. if (parent)
  73. parent.appendChild(tdCell);
  74. return tdCell;
  75. }
  76.  
  77. table = document.querySelector("table");
  78. table.innerHTML="";
  79. for (var i=0; i<contacts.length; i++) {
  80. var tr = document.createElement("tr");
  81. for (var k in contacts[i])
  82. if (contacts[i].hasOwnProperty(k))
  83. createCell(contacts[i][k], tr);
  84. table.appendChild(tr);
  85. tr.setAttribute("class", contacts[i].id);
  86. createCell("<button>Delete</button>",tr);
  87. }
  88. }
  89.  
  90. render(contactManager.contacts);
  91.  
  92. table.addEventListener("click", function(e) {
  93. var t = e.target;
  94. if (e.target.tagName.toLowerCase()!="button")
  95. return;
  96. var id = t.parentNode.parentNode.getAttribute("class");
  97. var contact = contactManager.find(id, "id");
  98. contactManager.contacts.splice(contactManager.contacts.indexOf(contact),1);
  99. render(contactManager.contacts);
  100. });
  101. </script>
  102. </body>
  103. </html>
  104. ---
  105.  
  106.  
  107.  
  108.  
  109. >>> car.name
  110.  
  111. "Corvette"
  112.  
  113. >>> car.print()
  114.  
  115. Car? is Corvette
  116.  
  117. index.html (line 16)
  118.  
  119. undefined
  120.  
  121. >>> truck = { name : "hummer" }
  122.  
  123. Object { name=
  124.  
  125. "hummer"
  126.  
  127. }
  128.  
  129. >>> truck.print = car.print
  130.  
  131. function()
  132.  
  133. >>> truck.print()
  134.  
  135. Car? is hummer
  136.  
  137. index.html (line 16)
  138.  
  139. undefined
  140.  
  141. >>> window.myPrint = car.print
  142.  
  143. function()
  144.  
  145. >>> myPrint()
  146.  
  147. Car? is
  148.  
  149. index.html (line 16)
  150.  
  151. undefined
  152.  
  153. >>> x= undefined
  154.  
  155. undefined
  156.  
  157. >>> if (x === undefined) alert("yes")
  158.  
  159. undefined
  160.  
  161. >>> if (x === null) alert("yes")
  162.  
  163. undefined
  164.  
  165.  
  166.  
  167. ----
  168.  
  169.  
  170.  
  171. >>> accessors.get()
  172.  
  173. "hello"
  174.  
  175. >>> accessors.set("different message")
  176.  
  177. undefined
  178.  
  179. >>> accessors.get()
  180.  
  181. "different message"
  182.  
  183.  
  184. >>> a = { id : 4 }
  185.  
  186. Object { id=
  187.  
  188. 4
  189.  
  190. }
  191.  
  192. >>> b = Object.create(a)
  193.  
  194. Object { id=
  195.  
  196. 4
  197.  
  198. }
  199.  
  200. >>> b.name = "X"
  201.  
  202. "X"
  203.  
  204. >>> b.name
  205.  
  206. "X"
  207.  
  208. >>> b.id
  209.  
  210. 4
  211.  
  212. >>> b.id = 7
  213.  
  214. 7
  215.  
  216. >>> b
  217.  
  218. Object { name=
  219.  
  220. "X"
  221.  
  222. , id=
  223.  
  224. 7
  225.  
  226. }
  227.  
  228. >>> a
  229.  
  230. Object { id=
  231.  
  232. 4
  233.  
  234. }
  235.  
  236. >>> delete b.id
  237.  
  238. true
  239.  
  240. >>> b
  241.  
  242. Object { name=
  243.  
  244. "X"
  245.  
  246. , id=
  247.  
  248. 4
  249.  
  250. }
  251.  
  252. >>> Object.getPrototypeOf(b)
  253.  
  254. Object { id=
  255.  
  256. 4
  257.  
  258. }
  259.  
  260. >>> Object.getPrototypeOf(b) === a
  261.  
  262. true
  263.  
  264. >>> b.name
  265.  
  266. "X"
  267.  
  268. >>> b.hasOwnProperty("name")
  269.  
  270. true
  271.  
  272. >>> b.hasOwnProperty("id")
  273.  
  274. false
  275.  
  276. >>> b.id
  277.  
  278. 4
  279.  
  280. >>> for (var k in b) { console.log(k) }
  281.  
  282. name
  283.  
  284. id
  285.  
  286. >>> c = Object.create(b)
  287.  
  288. Object { name=
  289.  
  290. "X"
  291.  
  292. , id=
  293.  
  294. 4
  295.  
  296. }
  297.  
  298. >>> d = Object.create(c)
  299.  
  300. Object { name=
  301.  
  302. "X"
  303.  
  304. , id=
  305.  
  306. 4
  307.  
  308. }
  309.  
  310. >>> c.foo = "monkey"
  311.  
  312. "monkey"
  313.  
  314. >>> d.foo
  315.  
  316. "monkey"
Add Comment
Please, Sign In to add comment