zoukankan      html  css  js  c++  java
  • 【学习】js学习笔记:对象的遍历和封装特性

    1、对象的属性访问:

    对象.属性

    对象[属性],但中括号中必须是字符串

    2、属性的遍历:

    for in方法举例:

    var ren={};

    ren.name="名字";

    ren.eat=function(){

             alert("吃饭");

             }

            

    for(var i in ren){

             alert(ren[i]);

             }

    3、封装-工厂函数,这种方式格式不规范

    function dianshi(color,size,brand){

                       var tv={};

                       tv.color=color;

                       tv.size=size;

                       tv.brand=brand;

                       tv.play=function(){

                                alert("玩");

                                }

                       tv.look=function(){

                                alert("看");

                                }

                       return tv;

                       }

            

             var d1=dianshi("red","42cun","sony");

             var d2=dianshi("blue","40cun","长虹");

             var d3=dianshi("green","30cun","康佳");     

            

             alert(d1.color);

             alert(d2.size);

             alert(d3.brand);

    4、封装-构造函数,这种方式格式更正式

    function tv(color,size,brand){          

                       this.color=color;

                       this.size=size;

                       this.brand=brand;

                       this.play=function(){

                                alert("玩");

                                }

                       this.look=function(){

                                alert("看");

                                }

                       }

            

             var d1=new tv("red","42cun","sony");

             var d2=new tv("blue","40cun","长虹");

             var d3=new tv("green","30cun","康佳");      

            

             alert(d1.color);

             alert(d2.size);

             alert(d3.brand);

    5、封装-代码段

    prototype方法:将共有的方法和属性添加到代码段中

    function tv(color,size,brand){          

                       this.color=color;

                       this.size=size;

                       this.brand=brand;            

                       this.look=function(){

                                alert("看");

                                }

                       }

                      

             tv.prototype.play=function(){

                                alert("玩");

                                }

            

             var d1=new tv();

             d1.play();

    6、混合方法:几种方式的混合,扬长避短

    function tv(color,size,brand){          

                       this.color=color;

                       this.size=size;

                       this.brand=brand;            

                       this.look=function(){

                                alert("看");

                                }

                       }

                      

             tv.prototype.obj={name:"名字"}    

                      

             tv.prototype.play=function(){

                                alert("玩");

                                }

            

             var d1=new tv();

            

             d1.play();

             alert(d1.obj.name);

  • 相关阅读:
    Git使用入门
    Android系统移植和驱动开发
    搭建Android开发环境
    Android源代码的下载和编译
    Andriod深度探索(卷1)HAL与驱动开发 第十章读书心得
    Andriod深度探索(卷1)HAL与驱动开发 第九章读书心得
    Andriod深度探索(卷1)HAL与驱动开发 第八章读书心得
    Android深度探索(卷1)HAL与驱动开发 第七章读书心得
    Android深度探索(卷1)HAL与驱动开发 第六章读书心得
    Android深度探索(卷1)HAL与驱动开发 第五章读书心得
  • 原文地址:https://www.cnblogs.com/xiaoxianweb/p/5726506.html
Copyright © 2011-2022 走看看