[渡一] 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
阅读全文
1 0
- [渡一] 171108 构造函数、包装类、临时对象、原型
- JavaScript之对象、构造函数、包装类
- 对象、构造函数、原型
- JavaScript面向对象与原型(一):构造函数
- 构造对象与原型一
- Javascript构造函数、原型、对象
- 构造函数和原型对象
- javascript对象+构造函数内部原理+包装类
- Javascript函数、构造函数、原型、类和对象
- 类、构造函数、原型
- 自动调用类的构造函数生成临时类对象
- 临时对象与拷贝构造函数
- 临时对象与拷贝构造函数
- javascript面向对象技术基础(四)(类、构造函数、原型)
- 类函数, 原型对象 与 构造方法之间的关系
- 面向对象,创建对象,构造函数,原型,原型链
- c++ 赋值构造函数 临时变量 临时对象 之一
- JS原型+构造函数创建对象
- Notepad这款笔记软件
- HDU3873:Invade the Mars(Dijkstra)
- 设计模式之使用序列化和反序列化实现单例模式
- imx6 android7.1.1 源码下载
- [Leetcode] 437. Path Sum III 解题报告
- [渡一] 171108 构造函数、包装类、临时对象、原型
- Android控件_SeekBar
- Java
- Windows版OpenVPN安装、配置教程
- 磁盘分区管理
- Maven项目导入出错,找不到依赖jar包问题
- 文章标题
- java线程的中断理解
- 当使用phpize的时候出现Can't find PHP headers in /usr/include/php The php-devel package is required for use o