zoukankan      html  css  js  c++  java
  • JavaScript Tutorial 02 #Object#

    var o3 = Object.create(Object.prototype); //equals new Object()
    //
    function inherit(p) {
        if(p==null) throw TypeError();
        if(Object.create) {
            return Object.create(p);
        }
    
        var t = typeof p;
        if(t!=="object" && t!=="function") throw TypeError();
        function f() {};
        f.prototype = p;
        return new f();
    }
    
    var o = {x:1}
    o.hasOwnProperty("x"); //true
    o.hasOwnProperty("y");
    o.hasOwnProperty("toString"); //false
    
    var o = {
        x: 1.0,
        y: 1.0,
        get r() {
            return Math.sqrt(this.x*this.x + this.y*this.y);
        },    
    }
    
    var serialnum = {
        $n : 0,
        get next() {
            return this.$n++;
        },
    }
    
    /* 给Object.prototype添加一个不可枚举的extend()方法 */
    Object.defineProperty(Object.prototype,
        "extend",
        {
            writable: true,
            enumerable: false,
            configurable: true,
            value: function(o) {
                var names = Object.getOwnPropertyNames(o);
                for(var i=0; i<names.length; i++) {
                    if(names[i] in this) continue;
                    var desc = Object.getOwnPropertyDescriptor(o,names[i]);
                    Object.defineProperty(this,names[i],desc);
                }
            }
        });
    
    /* prototype, class, extensible attribute */
    /* o.constructor.prototype */
    
    /* json */
    o = {x:1, y:{z:[false,null,""]}};
    s = JSON.stringify(o);
    p = JSON.parse(s);

  • 相关阅读:
    获取web应用路径 // "/" 表示class 根目录
    C++ Knowledge series Inheritance & RTTI & Exception Handling
    ATL
    GCC & Maker
    NoSQL(Not Only SQL)
    ECMAScript Regex
    C++ Knowledge series Conversion & Constructor & Destructor
    Cloud Computing
    C++ Knowledge series STL & Const
    Java Knowledge series 7
  • 原文地址:https://www.cnblogs.com/lambdatea/p/3376649.html
Copyright © 2011-2022 走看看