[渡一] 171108 构造函数、包装类、临时对象、原型

来源:互联网 发布:诺基亚n8 00软件下载 编辑:程序博客网 时间:2024/05/16 15:39

tips1.构造函数

  • 创建对象的方法:
var obj = {};//对象自变量的方式function Person() {}var oPerson1 = new Person();//构造函数创建对象
  • 构造函数名的单词首字母大写
  • 构造函数中,浏览器会默认创建一个“this”对象,并返回“this”
function PersonHouse(){   //var this = {       //name = 'duyi';       //age = 10;   //};   this.name = 'duyi';   this.age = 10;   //return this;}
  • 灵活构造函数并创建新对象
function PlaneFactory(size) {   switch (size) {       case "small":           this.blood = 100;           this.width = 60;           this.height = 60;           break;       case "medium":           this.blood = 150;           this.width = 70;           this.height = 80;           break;       case "large":           this.blood = 200;           this.width = 90;           this.height = 100;           break;   }   this.die = function() {}}var oPlane1 = new PlaneFactory("medium");

tips2.包装类、临时对象

这里写图片描述

  • 简单引出:“var str = “roc”; [str]是一个原始值,本不存在属性,现在查看[str.length], 得出结果[3]

  • 实际发生: new String(str).length 建立了一个新对象,没有变量去接,称之为【临时对象】。本行执行结束就会销毁。

  • ECMAScript 提供了 3 个特殊的引用类型:Boolean、Number和 String。这些类型与其他引用类型相似,但同时也具有与各自的基本类型相应的特殊行为。 
    实际上,每当读取一个基本类型值的时候,后台就会创建一个对应的基本包装类型的对象,从而能够调用一些方法来操作这些数据。

  • 包装对象: 
    包装对象,就是当基本类型以对象的方式去使用时,JavaScript会转换成对应的包装类型,相当于new一个对象,内容和基本类型的内容一样,然后当操作完成再去访问的时候,这个临时对象会被销毁,然后再访问时候就是undefined。number,string,boolean都有对应的包装类型。

  • String 类型 
    String 类型包含了三个属性和大量的可用内置方法。 
    String 对象属性

这里写图片描述 
eg:

//面试出坑题var str = "cst";str.name = "ccc"; console.log(str.name + "aaa"); //输出[undefinedaaa]//首先str是一个原始值,不存在属性//既然要查看属性,浏览器默认用String类型进行包装原始值  //用包装类将原始值创建成临时对象//何为临时对象,执行完一次就销毁,不影响下文代码//因此输出语句只看本行,console.log(String(str).name) 

eg:判断输入字符串字节数

  • 已知调用charCodeAt方法可以查看字符arsc码值
var str = "acsca中文哈哈aa";//new String() -> charCodeAt(1) -> "97"//255 -> 2  255- ->1function byteStr(selfStr) {    var allByteNum = 0;    for (var i = 0; i < selfStr.length; i++) {        if (selfStr.charCodeAt(i) > 255) {            allByteNum += 2;        } else {            allByteNum += 1;        }    }    return allByteNum;}console.log(byteStr(str));

tips3.原型

  • 原型prototype、作用域[[scope]]都是function对象的一个属性
  • 而作为属性的【原型】本身也是一个对象
  • 每一个对象都从原型继承属性
function Person() {      this.age = 20;      this.face = "amazing"}Person.prototype.lastname = "Deng";Person.prototype.money = 10000000;var oPerson = new Person();//自己的属性中没有的话,去找祖先的属性console.log(oPerson.lastname);
function catFactory(wheelNum,carColor){AO -> {wheelNum = undefined ,carColor = undefined ,this:undefined}                     3                    gray              {}//var this = {};this.door = 4;this.wheel = wheelNum;this.color = "carColor";this.distance = 0;}
function CarFactory(wheelNum, carColor) {    this.wheel = wheelNum;    this.color = carColor;}CarFactory.prototype.door = 4;CarFactory.prototype.distance = 0;CarFactory.prototype.run = function() {    this.distance += 100;}CarFactory.prototype.author = {    name: "cst",    money: 999999999}var oCar1 = new CarFactory(3, "gray");var oCar2 = new CarFactory(4, "red");oCar1.author.money = 200; //操作的是原型,因为属性是对象console.log(oCar2.author.money);oCar1.door = 6; //对象先看自己是否具有属性,没有看原型//只改变了自己的属性,原型没变 console.log(oCar1.door);console.log(delete oCar1.door); //对象里没有这个属性也可以删除返回true,原型没影响//目前阶段 无法通过对象改变原型oCar1.run();console.log(oCar1.distance);console.log(oCar2.distance);
  • 对象本身是否有要求的属性,没有就去原型中找
  • 删除对象里没有的属性也会返回[true]

    这里写图片描述

Person.prototype.name = "sunny";function Person() {}Person.prototype.name = "cherry";var oPerson = new Person();console.log(oPerson.name);function Person() {}Person.prototype.name = "sunny"; //->CarFactory.prototype指向一个无形的对象1{name:"sunny"}var oPerson = new Person(); //CarFactory.prototype(对象1)->oPerson.this.__proto__Person.prototype = {name: "cherry"}; //->CarFactory.prototype指向一个新建而非替换对象1的对象2{name:"cherry"}//Person.prototype.name = "cherry";console.log(oPerson.name);//__proto__浏览器给的属性:构造函数才有 -> var this = {__proto__:CarFactory.prototype}// oCar3.door -> if(oCar.door){return oCar3.__proto__door}
  • 主要看新对象建立的先后,在原型保持值1时,建立对象1,则对象的属性_proto_的值是原型1时的值
  • 若此时更新原型的值变成值2时,原型值改变,不影响_proto_的值
  • 若此时建立对象2,则对象2的原型值为原型2

这里写图片描述

原创粉丝点击