zoukankan      html  css  js  c++  java
  • js04---object1

    我们目前为止大多数引用类型都是Object类型的实例,Object也是ECMAScript中使用最多的一种类型(就像java.lang.Object一样,Object类型是所有它的实例的基础)。Object类型的创建方式、使用对于Object类型应用for in 枚举循环 
    
    
    Object每个实例都会具有下列属性和方法:
    Constructor: 保存着用于创建当前对象的函数。(构造函数)
    hasOwnProperty(propertyName):用于检测给定的属性在当前对象实例中(而不是原型中)是否存在。
    isPrototypeOf(Object): 用于检查传入的对象是否是另外一个对象的原型。
    propertyIsEnumerable(propertyName):用于检查给定的属性是否能够使用for-in语句来枚举。
    toLocaleString():返回对象的字符串表示。该字符串与执行环境的地区对应.
    toString():返回对象的字符串表示。
    valueOf():返回对象的字符串、数值或布尔表示。
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Untitled Document</title>
            <script type=text/javascript charset=utf-8>
    /*var obj = {} ;
    obj.say = function FF(){  //这里的FF外部是使用不了的
        alert('hello world!');                    
    }
    FF();//FF is not defined,相当于window.FF(),没有window.FF = function(){}
    var f = new FF();//FF is not defined  */
    
            //Object 所有类的基础类 
            var obj1 = new Object();
            var obj = {} ;        // 实例化对象,2中写法一样,
            // 给对象设置属性
            obj.name = '张3';
            obj.age  = 20 ; 
            obj.sex  = '';
            obj["birthday"] = '1980-08-07';
            obj.say = function(){  //匿名对象
                alert('hello world!');                    
            }
            // 访问对象的属性或方法
            alert(obj.name);
            alert(obj.age);
            obj.say();
            
            // delete 操作符 删除对象的属性或方法的
            delete obj.age ;
            delete obj.say ;
            alert(obj.name);
            alert(obj.age);//undefined
            alert(obj.sex);
            obj.say();//say is not a fuchtion
            
            //遍历js对象,便利json,
            for(var attribute in obj) {
                alert(attribute + typeof attribute +" : "+ obj[attribute]);  //obj[attribute]要用中括号,不能用点。
            }
            //name string : 张3,  age string : 20  ,sex string : 男 ,birthday string : 1980-08-07  ,say string : function(){alert('hello world!');}
            
            
            //Constructor保存对象的创建函数
            alert(obj.constructor);//function Onject(){}
            var arr = [] ; 
            alert(arr.constructor);//function Array(){}
            
            //hasOwnProperty(propertyName) 用于检测给定属性在对象中是否存在
            alert(obj.hasOwnProperty('sex'));
            
            //isPrototypeOf(Object) 检测原型 
            
            //检测给定的属性是否能被for in 所枚举出来 
            alert(obj.propertyIsEnumerable('say'));
            </script>
        </head>
        <body>
        </body>
    </html>
  • 相关阅读:
    Windows7在自由的虚拟机(微软官方虚拟机)
    POJ1135_Domino Effect(最短)
    项目优化经验分享(七)敏捷开发
    POJ 3299 Humidex(简单的问题)
    NEFUOJ 500 二分法+最大流量
    json 解析解乱码
    【设计模式】Abstract Factory模式
    汇编语言的应用
    Duanxx的STM32学习:NVIC操作
    PHP移动互联网的发展票据(6)——MySQL召回数据库基础架构[1]
  • 原文地址:https://www.cnblogs.com/yaowen/p/6860486.html
Copyright © 2011-2022 走看看