zoukankan      html  css  js  c++  java
  • javascript创建对象

    JavaScript中对象的创建有以下几种方式: 

    (1)使用内置对象 
    (2)使用JSON符号 
    (3)自定义对象构造 

    一、使用内置对象 

    JavaScript可用的内置对象可分为两种: 
    1,JavaScript语言原生对象(语言级对象),如String、Object、Function等; 
    2,JavaScript运行期的宿主对象(环境宿主级对象),如window、document、body等

    var str = new String("实例初始化String"); 
    var str1 = "直接赋值的String"; 
    var func = new Function("x","alert(x)");//示例初始化func 
    var o = new Object();//示例初始化一个Object 
    window.onload=function(){
                var arr=new Array();
                var colors = new Array("ds","dsddd");
                var str=new String("fds");
            console.log(str)
            
    }

    输出:String {0: "f", 1: "d", 2: "s", length: 3, [[PrimitiveValue]]: "fds"}

    二、自定义对象构造 

    创建高级对象构造有两种方式:使用“this”关键字构造、使用原型prototype构造

    //使用this关键字定义构造的上下文属性 
    function Girl() 
    { 
    this.name = "big pig"; //用this关键字
    this.age = 20; 
    this.standing; 
    this.bust; 
    this.waist; 
    this.hip; 
    } 
    
    //使用prototype 
    function Girl(){} 
    Girl.prototype.name = "big pig"; 
    Girl.prototype.age = 20; 
    Girl.prototype.standing; 
    Girl.prototype.bust; 
    Girl.prototype.waist; 
    Girl.prototype.hip; 
    alert(new Girl().name); 

    //使用工厂函数
     function Car(color,door){ 
      var ocar = new Object; //利用对象来创建
      ocar.color = color; 
      ocar.doors = door; 
      ocar.showColor = function(){ 
      document.write(this.color) 
      }; 
      return ocar; 
      } 
    var car1 = Car("red",4); 

    封装js工具类

    <<愿你被这世界温柔以待...>>
  • 相关阅读:
    中国黑客传说:游走在黑暗中的精灵
    智能硬件安全入门
    迈克菲:2016年的八大网络安全威胁
    走进科学之WAF(Web Appllication Firewall)篇
    从对SAE的一次授权安全评估浅谈云安全
    沟通的艺术,心理学与生活,学会提问
    知道创宇研发技能表v3.0
    SYN Cookie的原理和实现
    1043. 输出PATest(20)
    1042. 字符统计(20)
  • 原文地址:https://www.cnblogs.com/NotePad-chen/p/7345018.html
Copyright © 2011-2022 走看看