javascript入门笔记(4)——对象
2015-08-18 10:35
501 查看
Object
Objectoperator
delete
traverse all atrributes
Construction
prototype object
problems
solution
![](https://lh3.googleusercontent.com/-dIjkWbhlncQ/VcTferhYXqI/AAAAAAAAAAw/RToiIgJXLCs/s0/OBJECT.png)
like pairs of key:value
var o = new Object(); var circle = {x:0,y:0, radius:2};
operator
you can dynamically add more attributes and child objects!var book = new Object(); book.title = "html study"; //you can dynamically add more attributes and child objects! book.chapter1 = new Object(); book.chapter1.title = "html introduction";
delete
delete book.chapter1; book.chapter1 = null;
traverse all atrributes
for( var x in o)…var o = new Object(); o.name = "john"; o.age = "56"; for(var x in o){ alert(o[x]);//john,56 alert(x);//name,age alert(x.valueOf());//name,age alert(o.x);//undefined. cuz o doesn't have x attribute }
Construction
![](https://lh3.googleusercontent.com/-rlI-3_fGdp0/VcTnRpoi6LI/AAAAAAAAABE/SqGOgGNgYOQ/s0/constructionObject.png)
Attention:
this.area = function(){ return this.width*this.height; }
if you change it like this:
this.area= function(){ return w*h; }
there could be a problem.
eg:
var r = new Rect(5,10); r.width = 10; alert(r.area());//this must update with width
prototype object
shared attributefunction Person(){ Person.prototype.name = "jane"; }; var p1 = new Person(); var p2 = new Person(); p1.name = "john";// assignment alert(p1.name);//john (from instance) alert(p2.name);//jane (from prototype)
problems
![](https://lh3.googleusercontent.com/-w97ioTllVtc/VcTwCxKEzUI/AAAAAAAAABY/dCFENTMZYFY/s0/problem.png)
if the attribute is an array, the push() action will affect all the objects.
solution:
combine prototype and regular construction.![](https://lh3.googleusercontent.com/-GD0hVDLr2GY/VcTw_TMFXII/AAAAAAAAABk/WCvlUqUKkxE/s0/solution.png)
相关文章推荐
- javascript入门笔记(3)——数组
- js实现iPhone界面风格的单选框和复选框按钮实例
- javascript入门笔记(2)——判断、循环和函数
- JavaScript 随机数函数
- Webform服务器控件调用JS
- js 中var转int
- Webfrom 生成流水号 组合查询 Repeater中单选与复选控件的使用 JS实战应用
- javascript入门笔记(1)——变量和计算
- javascript中的面向对象(1)
- EmberJs之数组绑定@each&[]
- js 小数取整的函数
- JS中的prototype
- js模拟淘宝网的多级选择菜单实现方法
- javascript基金会——鼠标事件,系统对话框,等等。
- javascript立即执行函数写法
- hdoj1869 六度分离(dijstra——floyd)
- hdoj 2544 最短路(dijstra模板)
- Newtonsoft.Json高级用法(转载)
- ExtJs在disabled和readOnly美学分析
- jstree树形菜单