В данном посте посмотрим как работать со свойствами объектов – проверять есть ли они, добавлять новые, удалять существующие.
Main.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>main</title> </head> <body> <button id="showproperties">showproperties</button> <button id="RedefineProperty">RedefineProperty</button> <button id="AddNewProperty">AddNewProperty</button> <button id="DeleteProperty">DeleteProperty</button> <button id="AddMethod">AddMethod</button> <button id="DeleteMethod">DeleteMethod</button> <script src="EventHandler.js"></script> <hr> <div id="mydiv"> </div> </body> </html> |
EventHandler.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
/** * @author Stas */ //Стандартный конструктор объекта function Dog(name,breed,weight){ this.name=name; this.breed=breed; this.weight=weight; this.bark=function(){alert(this.name)+'says WOOF!';}; } //Создаем объект var fido=new Dog('Fido','Mixed',36); //Показываем свойства в div function showproperties(){ //Очистка div document.getElementById('mydiv').innerHTML=''; for (var p in fido){ document.getElementById('mydiv').innerHTML= document.getElementById('mydiv').innerHTML+ p+':'+fido[p]+'<br>'; } }; //Добавление свойства function AddNewProperty(){ var NewPropertyName=prompt('Type the name of the new property','NewPropertyName'); var NewPropertyValue=prompt('Type the value of the new property','NewPropertyValue'); for (p in fido){if (p==NewPropertyName){alert('Such Property Exists'); return;} // else {fido[NewPropertyName]=NewPropertyValue;} } }; //Удаление свойства function DeleteProperty(){ var DeletePropertyName= prompt('Type the name of the deleting property','DeletePropertyName'); for (p in fido){if (p==DeletePropertyName) {delete fido[DeletePropertyName];} } } //Проверяем и переопределяем свойства function RedefineProperty(){ if (fido.hasOwnProperty('name')){fido.name=prompt('Give a new name','NewFido');}; showproperties(); }; //Добавление метода function AddMethod(){ fido.NewMethod=function(){alert('That is demo of new added method');}; showproperties(); fido.NewMethod(); } //Удаление метода function DeleteMethod(){ if (fido.hasOwnProperty('NewMethod')){delete fido.NewMethod;}; showproperties(); } document.getElementById('showproperties').onclick=showproperties; document.getElementById('RedefineProperty').onclick=RedefineProperty; document.getElementById('AddNewProperty').onclick=AddNewProperty; document.getElementById('DeleteProperty').onclick=DeleteProperty; document.getElementById('AddMethod').onclick=AddMethod; document.getElementById('DeleteMethod').onclick=DeleteMethod; |