zoukankan      html  css  js  c++  java
  • 08.03 js _oop

    js 分6个基本类型: string boolean number undefind null   自定义对象

    对象的种类

    1. js内置的  ( 比如 string number )
    2. 宿主对象 (比如  window )
    3. 自己创建的

    对象的创建:

    var book1 = {};     // 字面量的方式创建对象
    
    var book2 = new Object();
    
    var book2 = new Object; 
    

      以上是对象创建的三种方法。

    *  如果new Object()中没有传入参数,与{}是一样的。
       但是如果传入不同的参数,会有不同的效果。

    • 传入String 返回String,类似new String()
    • 传入Number 返回Number,类似new Number()
    • 传入Object 返回Object,其实没啥用

    当然还有其他的,比如传入数组等等~基本都是返回传入的类型,并且传入前后的对象是不变的,也就是不会进行一次拷贝。也就是如下代码

      var a = {"name" : "Jlp"}
      var b = new Object(a);
    
    此时: a === b  // 判断对象时  == 和 === 是一样的
     
    var myClass = {
    "name" : "jlp",
    "work" : function(){console.log("working......")},
    "_age" : 18,
    get age(){
    return this._age;   //如果不加 this ,报错 undefined。
    },
    set age(val){
    if (val < 0 || val>150){
    throw new Error("invald age you set!");
    }else{
    this._age = val;
    }
    }
    }

    对象成员的访问: (例如 直接访问 object.1  )数字,是不行的

    var o={1:abc}
    console.log(o.1) //出错
    console.log(o[1]) //可以

    console.log(myClass && myClass.address && myClass.address.home ) //  如果全部都有值的话,就返回  myClass.address.home


     

    构造器自创建函数:

    Object :      defineProperty  和  defineProperties

    defineProperty : defineProperty(object,"subParam",{value,writable,enumerable,configurable})
    /* MSDN 官方例子! */

    var
    newLine = "<br />"; // Create a user-defined object. var obj = {}; // Add a data property to the object. Object.defineProperty(obj, "newDataProperty", { value: 101, writable: true, enumerable: true, configurable: true }); // Set the property value. obj.newDataProperty = 102; document.write("Property value: " + obj.newDataProperty + newLine); // Output: // Property value: 102

     

  • 相关阅读:
    5.2-5.3
    5.1封装
    阅读《构建之法》 5-7章
    做汉堡
    阅读《构建之法》1-5章
    结对 四则运算
    回答
    读后感
    提问*2
    提问1
  • 原文地址:https://www.cnblogs.com/Tachi/p/5732905.html
Copyright © 2011-2022 走看看