JavaScript 对象原型链的简要说明

    称之为原型链,有如下特点:

    1. __proto__ 为对象内部的隐藏属性
    2. __proto__ 为实例化该对象的构造器的 prototype 对象的引用,因此可以直接方法 prototype 的所有属性和方法
    3. 除了 Object 每个对象都有一个 __proto__ 属性且逐级增长形成一个链,原型链顶端是一个 Object 对象。
    4. 在调用属性或方法时,引擎会查找自身的属性如果没有则会继续沿着原型链逐级向上查找,直到找到该方法并调用。
    5. __proto__ 跟浏览器引擎实现相关,不同的引擎中名字和实现不尽相同(chrome、firefox中名称是 __proto__ ,并且可以被访问到,IE中无法访问)。基于代码兼容性、可读性等方面的考虑,不建议开发者显式访问 __proto__ 属性或通过 __proto__更改原型链上的属性和方法,可以通过更改构造器prototype 对象来更改对象的 __proto__ 属性。

    构造器对象与普通对象的区别

    1. 构造器对象原型链中的 __proto__ 是一个 Function.prototype 对象的引用,因此可以调用 Function.prototype的属性及方法
    2. 构造器对象本身有一个 prototype 属性,用该构造器实例化对象时该 prototype 会被实例对象的 __proto__ 所引用
    3. 构造器对象本身是一个 function 对象,因此也会有自身属性

    构造器对象

    • Object
    • Boolean
    • String
    • Number
    • Function
    • Array
    • RegExp
    • Date
    • Error

    其他对象

    • Math
    • JSON
    • 全局对象

    内置对象,其实也叫内置构造器,它们可以通过 new 的方式创建一个新的实例对象。内置对象所属的类型就叫内置对象类型。其声明方式如下:

    1. var i = new String("str"); // String Object
    2. var h = new Number(1); // Number Object
    3. var g = new Boolean(true); // Boolean Object
    4. var j = new Object({name : "Tom"}); // Object Object
    5. var k = new Array([1, 2, 3, 4]); // Array Object
    6. var l = new Date(); // Date Object
    7. var m = new Error();
    8. var n = new Function();
    9. var o = new RegExp("\\d");

    注意:虽然标准类型中有Boolean String Number Object,内置对象类型中也有Boolean String Number Object,但它们其实是通过不同的声明方式来进行区别的。标准类型通过直接赋值,而对象类型则是通过构造器实现初始化。

    Object

    用 String/Number 等构造器创建的对象原型链顶端对象始终是一个Object对象,因此这些对象可以调用Object的原型对象属性和方法。所以 String/Number 等构造器是 Object 的子类。

    更多关于 Object 的内容可以在这里找到。

    构造器说明

    • Object 是属性和方法的集合
    • String/Number/Boolean/Array/Date/Error 构造器均为 Object 的子类并集成 Object 原型对象的属性及方法。

    实例化方法

    1. var obj0 = new Object({name: 'X', age: 13});
    2. // 常用方法
    3. var obj1 = {name: 'Q', age: 14};

    属性及方法

    • prototype
    • create
    • keys

    **原型对象属性及其方法

    • constructor
    • toString
    • valueOf
    • hasOwnProperty

    实例对象属性及方法

    Object.create

    功能:基于原型对象创造新对象

    1. // Object.create(prototype[, propertiesObject])
    2. var prototype = {name: 'X', age: 13};
    3. var obj = Object.create(proto);

    Object.prototype.toString

    功能:获取方法调用者的标准类型

    1. // objectInstance.toString()
    2. var obj = {};
    3. obj.toString(); // Object

    Object.prototype.hasOwnProperty

    功能:判断一个属性是否是一个对象的自身属性

    1. // objectInstance.hasOwnProperty("propertyName")
    2. var obj = Object.create({a: 1, b: 2});
    3. obj.c = 3;
    4. obj.hasOwnProperty('a'); // false
    5. obj.hasOwnProperty('c'); // true

    Boolean

    构造器说明:值为 true 与 false

    属性及方法

    • prototype

    **原型对象属性及其方法

    • constructor, toString, valueOf

    构造器说明:单双引号内的字符串

    实例化方法

    1. 'Hello, world!'
    2. var str0 = 'Xinyang';
    3. var str1 = new String('Xinyang');

    属性及方法

    • prototype
    • fromCharCode(转换 ASCII 代码为字符)
    • constructor
    • indexOf
    • replace
    • slice
    • split
    • charCodeAt
    • toLowerCase

    String.prototype.indexOf

    功能:获取子字符串在字符串中的索引

    1. // stringObject.indexOf(searchValue, fromIndex)
    2. var str = "I am X. From China!";
    3. var index = str.indexOf('a'); // 2
    4. str.indexOf('a', index + 1); // 16
    5. str.indexOf('Stupid'); // -1 字符串不存在

    String.prototype.replace

    功能:查找字符串替换成目标文字

    1. // stringObject.replace(regexp/substr, replacement)
    2. var str = "apple is bad";
    3. str = str.replace('bad', 'awesome');

    String.prototype.split

    功能:按分隔符将分隔符分成字符串数组

    1. // stringObject.split(separator, arrayLength)
    2. var str = '1 2 3 4';
    3. str.split(' '); // ['1', '2', '3', '4'];
    4. str.split(/\d+/); // ["", " ", " ", " ", ""]

    Number

    构造器说明:整型直接量,八进制直接量(0-),十六进制直接量(0x-),浮点型直接量

    实例化方法

    1. 10
    2. 1.2e5
    3. var count = 0x10;
    4. var pi = new Number(3.1415);

    属性及方法

    • prototype
    • MAX_VALUE
    • MIN_VALUE
    • NaN
    • NEGATIVE_INFINITY
    • POSITIVE_INFINITY

    原型对象属性及其方法

    • constructor
    • toFixed
    • toExponential

    Number.prototype.toFixed

    功能:四舍五入至指定小数位

    Array

    构造器说明:定义数组对象

    实例化方法

    1. var a0 = [1, 'abc', true, function(){}];
    2. var a1 = new Array();
    3. var a2 = new Array(1, 'abc', true);

    属性及方法

    • prototype
    • isArray

    原型对象属性及其方法

    • constructor
    • splice
    • forEach
    • find
    • concat
    • pop
    • push
    • reverse
    • shift
    • slice

    Array.prototype.splice

    功能:从数组中删除或添加元素,返回被删除的元素列表(作用域原有数组)

    1. var arr = ['1', '2', 'a', 'b', '6'];
    2. var ret = arr.splice(2, 2, '3', '4', '5'); // ['a', 'b']
    3. arr; // ['1', '2', '3', '4', 5', '6']

    Array.prototype.forEach

    功能:遍历元素组并调用回调函数

    1. // arrayObject.forEach(callback[, thisArg])
    2. // 回调函数
    3. // function callback(value, index, arrayObject) {...}
    4. // value - 当前值 index - 当前索引 arrayObject - 数组本身
    5. function logArray(value, index, arrayObject) {
    6. console.log(value);
    7. console.log(value === array[index]);
    8. }
    9. [2, 5, 6, 9].forEach(logArray);

    构造器说明:定义函数或新增对象构造器

    实例化方法

    1. // 对象实例化
    2. var f0 = new Function('i', 'j', 'return (i + j)');
    3. // 函数关键字语句
    4. function f1(i, j){return i + j;}
    5. // 函数表达式
    6. var f3 = function(i, j){return i + j;};

    属性及方法

    • prototype

    原型对象属性及其方法

    • constructor
    • apply
    • call
    • bind

    实例对象属性和方法

    • length
    • prototype
    • arguments
    • caller

    自定义对象构造器

    下面的代码声明一个 Point 增加了一个move方法,最后创建了一个 Point 的实例对象。

    1. function Point(x, y) {
    2. this.x = x;
    3. this.y = y;
    4. }
    5. Point.prototype.move = function(x, y) {
    6. this.x += x;
    7. this.y += y;
    8. }
    9. var p = new Point(1, 2);

    Function.prototype.apply

    功能:通过参数指定调用者和函数参数并执行该函数

    1. // functionObj.apply(thisArg[, argsArray])
    2. function Point(x, y) {
    3. this.x = x;
    4. this.y = y;
    5. }
    6. Point.prototype.move = function(x, y) {
    7. this.x += x;
    8. this.y += y;
    9. }
    10. var p = new Point(1, 1);
    11. var circle = {x: 1, y: 1, r: 1};
    12. p.move.apply(circle, [2, 1]); // {x: 3, y: 2, r: 1}

    Function.prototype.bind

    功能:通过参数指定函数调用者和函数参数并返回该函数的引用

    1. // functionObj.bind(thisArg[, arg1[, arg2[, ...]]])
    2. function Point(x, y) {
    3. this.x = x;
    4. this.y = y;
    5. }
    6. Point.prototype.move = function(x, y) {
    7. this.x += x;
    8. this.y += y;
    9. }
    10. var p = new Point(1, 1);
    11. var circle = {x: 1, y: 1, r: 1};
    12. var circleMoveRef = p.move.bind(circle, 2, 1);
    13. setTimeout(circleMoveRef, 1000); // {x: 3, y: 2, r: 1}
    14. // 之间使用 circleMoveRef() 效果等同于 apply()
    15. circleMoveRef();

    子类构造器

    1. function Circle(x, y, r) {
    2. Point.apply(this, [x, y]);
    3. this.radius = r;
    4. }
    5. Circle.prototype = Object.create(Point.prototype);
    6. Circle.prototype.constructor = Circle;
    7. Circle.prototype.area = function(){
    8. return Math.PI * this.radius * this.radius;
    9. }
    10. var c = new Circle(1, 2, 3);
    11. c.move(2, 2);
    12. c.area();

    函数调用

    • ()
    • apply

    函数参数

    • 形参个数不一定等于实参个数
    • 值专递
    arguments

    arguments 的常用属性

    • length 实参个数
    • 0…arguments.length-1 实参属性名称(key)
    • callee 函数本身
    1. function max(a, b) {
    2. if (max.length === arguments.length) {
    3. return a>b?a:b;
    4. } else {
    5. var _max = arguments[0];
    6. for(var i = 0; i < arguments.length; i++) {
    7. if (_max < arguments[i]) {
    8. _max = arguments[i];
    9. }
    10. }
    11. return _max;
    12. }
    13. }
    值专递

    函数参数的值专递是参数复制都是栈内存中的复制。

    内置对象 - 图2

    1. // 原始类型
    2. function plusplus(num) {
    3. return num++;
    4. }
    5. var count = 0;
    6. var result = plusplus(count); // result = 1; count = 0;
    7. // 引用类型
    8. function setName(obj) {
    9. obj.name = 'obama';
    10. }
    11. var president = {name: 'bush'};
    12. setName(president); // {name: 'obama'};
    函数重载

    Require.JS 中的 define() 为例:

    RegExp

    定义方法

    • /pattern/flags
    • new RegExp(pattern[, flags]);

    属性及方法

    • prototype

    原型对象属性及其方法

    • constructor
    • test
    • exec

    RegExp.prototype.test

    功能:使用正则表达式对字符串进行测试,并返回测试结果

    1. // regexObj.text(str)
    2. var reg = /^abc/i;
    3. reg.test('Abc123'); // true
    4. reg.test('1Abc1234'); // false

    Date

    构造器说明:用于定义日期对象

    定义方法

    1. var date0 = new Date();
    2. var date1 = new Date(2014, 3, 1, 7, 1, 1, 100);

    属性及方法

    • prototype
    • parse
    • now

    原型对象属性及其方法

    • constructor
    • Date
    • getDate
    • getHours
    • setDate
    • setHours

    Math

    对象说明:拥有属性和方法的单一对象主要用于数字计算

    对象属性

    • E
    • PI
    • SQRT2

    对象方法

    • floor
    • random
    • abs
    • max
    • cos
    • ceil
    Math.floor

    功能:向下取整

    1. // Math.floor(num)
    2. Math.floor(0.97); // 0
    3. Math.floor(5.1); // 5
    4. Math.floor(-5.1); //6

    相似方法:ceilround

    Math.random

    功能:返回 0~1 之间的浮点数

    1. // Math.random()
    2. Math.random(); // 0.14523562323461

    JSON

    对象说明:用于存储和交换文本信息

    对象方法

    • parse
    • stringify
    JSON.stringify

    功能:将 JSON 对象转换为字符转

    1. // JSON.stringify(value[, replacer[, space]])
    2. var json = {'name': 'X'};
    3. JSON.stringify(json); // "{"name":"X"}"
    JSON.parse

    功能:将 JSON 字符转转换为对象

    1. // JSON.parse(text[, reviver])
    2. var jsonStr = '{"name":"X"}';
    3. JSON.parse(jsonStr); // {name: 'X'}

    全局对象

    全局对象定义了一系列的属性和方法在编程过程中可以被之间调用。

    属性:NaN,Infinity,undefined

    方法:

    • parseInt
    • parseFloat
    • isNaN
    • isFinite
    • eval

    处理 URI 方法:

    • encodedURIComponent
    • decodeURIComponent
    • encodedURI
    • decodeURI

    构造器属性:

    • Boolean
    • String
    • Number
    • Object
    • Function
    • Array
    • Date
    • Error

    对象属性:

    • Math
    • JSON
    NaA

    非数字值:表示错误或无意义的运算结果,NaN 参与运算仍会返回 NaA,且 NaN 不等于任何值,包括它本身。可以使用 isNaN() 判断运算结果的类型是否为 NaN。

    1. isNaN(NaN); // true
    2. isNaN(4 - '2a'); // true;
    parseInt

    功能:转换字符串成数字

    1. // parseInt(string[, radix])
    2. // radix - 为进制数
    3. parseInt('010'); // 10
    4. parseInt('010', 8) // 8
    5. parseInt('010', 16) // 16
    6. parseInt('0x1f'); // 31
    7. parseInt('0x1f', 16); // 31
    8. parseInt('1f'); // 1
    9. parseInt('1f', 16); // 31
    eval
    1. // eval(string)
    2. var res = '{"error": "0", "msg": "OK"};
    3. var obj;
    4. if (!JSON) {
    5. obj = eval('(' + res + ')');
    6. } else {
    7. obj = JSON.parse(res);
    8. }
    encodedURIComponent

    功能:将 URI 参数中的特殊字符,中文等作为 URI 的一部分进行编码

    1. var uri = "http://w3schools.com/my test.asp?name=ståle&car=saab";
    2. var res = encodeURIComponent(uri);
    3. // http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab