Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>lab</title>
- </head>
- <body>
- <table border="1"></table>
- <input id="lastname" type="text"/><button id="find">Click to find</button>
- <div id="output"></div>
- <script>
- contactManager = {
- protoContact : {
- toString : function() {
- return this.first + " " + this.last + " " + this.email;
- }
- },
- Contact : function() {},
- contacts : [],
- build : function(params) {
- this.Contact.prototype = this.protoContact;
- var newContact = new this.Contact();
- for (var k in params)
- newContact[k] = params[k];
- newContact.id = Math.random();
- this.contacts.push(newContact);
- },
- find : function(val, field) {
- if (field === undefined)
- field = "last";
- for (var i=0; i<this.contacts.length; i++)
- if (this.contacts[i][field] == val)
- return this.contacts[i];
- }
- };
- test = function() {
- contactManager.build({ first:"Joe1", last:"Smith1", email: "joesmith1@gmail.com"});
- contactManager.build({ first:"Joe2", last:"Smith2", email: "joesmith2@gmail.com"});
- contactManager.build({ first:"Joe3", last:"Smith3", email: "joesmith3@gmail.com"});
- c1 = contactManager.find("Smith2");
- console.log(c1);
- document.getElementById("output").innerHTML = "Found " + c1.email;
- c2 = contactManager.find("Joe3", "first");
- console.log(c2);
- document.getElementById("output").innerHTML += "<br/>Found " + c2.email;
- }
- test();
- document.getElementById("find").addEventListener("click", function() {
- var found = contactManager.find(document.getElementById("lastname").value);
- document.getElementById("output").innerHTML +=
- "<br/>" + found.toString();
- var row = document.getElementsByClassName(found.id)[0];
- row.style.background="yellow";
- setTimeout(function(){row.style.background="";},1000);
- });
- h1 = document.createElement("h1");
- h1.innerHTML="Contacts";
- document.body.insertBefore(h1, document.querySelector("input"));
- render = function(contacts) {
- createCell = function(text, parent) {
- var tdCell = document.createElement("td");
- tdCell.innerHTML = text;
- if (parent)
- parent.appendChild(tdCell);
- return tdCell;
- }
- table = document.querySelector("table");
- table.innerHTML="";
- for (var i=0; i<contacts.length; i++) {
- var tr = document.createElement("tr");
- for (var k in contacts[i])
- if (contacts[i].hasOwnProperty(k))
- createCell(contacts[i][k], tr);
- table.appendChild(tr);
- tr.setAttribute("class", contacts[i].id);
- createCell("<button>Delete</button>",tr);
- }
- }
- render(contactManager.contacts);
- table.addEventListener("click", function(e) {
- var t = e.target;
- if (e.target.tagName.toLowerCase()!="button")
- return;
- var id = t.parentNode.parentNode.getAttribute("class");
- var contact = contactManager.find(id, "id");
- contactManager.contacts.splice(contactManager.contacts.indexOf(contact),1);
- render(contactManager.contacts);
- });
- </script>
- </body>
- </html>
- ---
- >>> car.name
- "Corvette"
- >>> car.print()
- Car? is Corvette
- index.html (line 16)
- undefined
- >>> truck = { name : "hummer" }
- Object { name=
- "hummer"
- }
- >>> truck.print = car.print
- function()
- >>> truck.print()
- Car? is hummer
- index.html (line 16)
- undefined
- >>> window.myPrint = car.print
- function()
- >>> myPrint()
- Car? is
- index.html (line 16)
- undefined
- >>> x= undefined
- undefined
- >>> if (x === undefined) alert("yes")
- undefined
- >>> if (x === null) alert("yes")
- undefined
- ----
- >>> accessors.get()
- "hello"
- >>> accessors.set("different message")
- undefined
- >>> accessors.get()
- "different message"
- >>> a = { id : 4 }
- Object { id=
- 4
- }
- >>> b = Object.create(a)
- Object { id=
- 4
- }
- >>> b.name = "X"
- "X"
- >>> b.name
- "X"
- >>> b.id
- 4
- >>> b.id = 7
- 7
- >>> b
- Object { name=
- "X"
- , id=
- 7
- }
- >>> a
- Object { id=
- 4
- }
- >>> delete b.id
- true
- >>> b
- Object { name=
- "X"
- , id=
- 4
- }
- >>> Object.getPrototypeOf(b)
- Object { id=
- 4
- }
- >>> Object.getPrototypeOf(b) === a
- true
- >>> b.name
- "X"
- >>> b.hasOwnProperty("name")
- true
- >>> b.hasOwnProperty("id")
- false
- >>> b.id
- 4
- >>> for (var k in b) { console.log(k) }
- name
- id
- >>> c = Object.create(b)
- Object { name=
- "X"
- , id=
- 4
- }
- >>> d = Object.create(c)
- Object { name=
- "X"
- , id=
- 4
- }
- >>> c.foo = "monkey"
- "monkey"
- >>> d.foo
- "monkey"
Add Comment
Please, Sign In to add comment