javascript 学习笔记

JavaScript语言精粹


======================================================================================================

JavaScript语言精粹

javascript 的简单类型包括:number、string、bool、null、undefined值。它们是不可变的。
其他类型都是对象,如数组、函数等。js对象是无类别的
对象字面量,{}创建对象,即:json数据格式

/* 1.属性名称是合法的,无需用引号括起来。 2.数据值随意 */ var stooge = { "first-name": "Jerome", last_name: "Howard", departure: { IATA: "SYD", time: "2004-09-22 14:55", city: "Sydney" } };/* 检索 1. 属性名称是一个合法的,可以用 ".",也可以用[]数组表示 2. 属性名称不是一个合法的,只能用数组[]表示 */ stooge['first-name'] stooge.last_name /* 检索一个不存在的成员的值,返回 undefined值 */ stooge["middle-name"] // undefined flight.status // undefined /* ||运算符可以用来填充默认值 */ var middle = stooge["middle-name"] || "(none)"; var status = flight.status || "unknown"; /* 检索一个undefined 值将会导致 TypeError 异常,可以通过 && 运算符避免错误 */ flight.equipment // undefined flight.equipment.model // throw "TypeError" flight.equipment && flight.equipment.model // undefined/* 更新: 如属性名已经存在,则更新,没有则添加属性 */ /* 引用: 对象通过引用来传递。它们永远不会被拷贝 */ var x = stooge; x.nickname = 'Curly'; var nick = stooge.nickname; //因为x 和 stooge 是指向同一个对象的引用,所以nick为 'Curly' var a = {}, b = {}, c = {}; // a, b, and c 都引用一个不同空对象 a = b = c = {}; // a, b, and c 都引用同一个空对象 /* 原型 :每个对象都连接到一个原型对象,并且它可以从中继承属性。 所有通过对象字面量创建的对象都连接到 Object.prototype 函数对象的对象原型是 Function.prototype(它的本身原型也是Object.prototype) */ /* 放射(Reflection): 用于确定对象属性类型 1. typeof 操作符用于确定属性类型 2. hasOwnProperty 方法,用于确定对象是否有独有的属性 */ typeof flight.number // 'number' typeof flight.status // 'string' typeof flight.toString // 'function' flight.hasOwnProperty('number') // true 检测到,flight有number属性 flight.hasOwnProperty('constructor') // false /* 枚举(Enumeration) for in 语句可用来遍历一个对象中所有属性名。属性名出现的顺序不确定的 */ var name; for (name in another_stooge) { if (typeof another_stooge[name] !== 'function') { document.writeln(name + ': ' + another_stooge[name]); } } /* 删除 delete 运算符操作符,可以用来删除对象的属性。它不会移除对象原型链中的任何*/ delete another_stooge.nickname; /* 减少全局变量 - 在程序中只创建一个全局变量,作为容器 */

函数
函数用于指定对象的行为。

/*函数对象 1. 函数对象的对象原型是 Function.prototype(它的本身原型也是Object.prototype) 2. 每个函数在创建时,附有两个附加的隐藏属性:函数的上下文和实现函数的代码。 每个函数在创建时也随带有一个prototype 属性。它的值是一个拥有constructor 属性且值即为该函数的对象。 3. 函数特殊在于可以被调用 */ //函数字面量创建 var add = function (a, b) { return a + b; }; /* 调用 1. 调用一个函数将暂停当前函数的执行,传递控制权和参数给新函数。除了声明时定义的形式参数,每个函数还附加两个参数:this和argument。 2. 参数 this 在面向对象编程中非常重要,它的取值决定于调用模式。 3. js四种调用模式:方法调用模式、函数调用模式、构造函数模式和apply调用模式。在如何初始化关键参数this上存在差异 */ /* 3.1 方法调用模式:*/ var myObject = { value: 0; increment: function (inc) { this.value += typeof inc === 'number' ? inc : 1; //this 绑定到myObject该对象 } }; /* 3.2 函数调用模式 this 被绑定到全局对象。这是语音设计上的错误。应该被绑定到外部函数的this 变量 解决办法是,this赋值给一个定义变量,那么内部函数就可以通过那个变量访问到this */ myObject.double = function ( ) { var that = this; //解决办法 var helper = function ( ) { that.value = add(that.value, that.value) }; helper( ); // Invoke helper as a function. }; /* 构造函数模式 在函数前面带上 new 来调用,那么将创建一个隐藏连接到该函数的 prototype 成员的新成员,同时 this 将会绑定到那个新对象上。 */ var Quo = function (string) { this.status = string; }; Quo.prototype.get_status = function ( ) { return this.status; }; var myQuo = new Quo("confused"); document.writeln(myQuo.get_status( )); // confused /* apply调用模式 apply 方法 */ /* 异常 throw try catch throw 语句中断函数的执行。它抛出一个exception 对象 try 语句只会捕捉抛出一个exception 对象 */ var add = function (a, b) { if (typeof a !== 'number' || typeof b !== 'number') { throw { //抛出一个exception 对象 name: 'TypeError', message: 'add needs numbers' } } return a + b; } var try_it = function ( ) { try { add("seven"); } catch (e) { document.writeln(e.name + ': ' + e.message); } } tryIt( ); /* 异常 throw try catch throw 语句中断函数的执行。它抛出一个exception 对象 try 语句只会捕捉抛出一个exception 对象 */ var add = function (a, b) { if (typeof a !== 'number' || typeof b !== 'number') { throw { //抛出一个exception 对象 name: 'TypeError', message: 'add needs numbers' } } return a + b; } var try_it = function ( ) { try { add("seven"); } catch (e) { document.writeln(e.name + ': ' + e.message); } } tryIt( ); /* 给类型增加方法 通过给Object.prototype 添加方法使得所有对象可用。同样的方式对函数、数组、字符串、数字正则表达式和布尔值同样实用 */ Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; }; Number.method('integer', function ( ) { return Math[this < 0 ? 'ceiling' : 'floor'](this); }); document.writeln((-10 / 3).integer( )); // -3 String.method('trim', function ( ) { return this.replace(/^\s+|\s+$/g, ''); }); document.writeln('"' + " neat ".trim( ) + '"'); /* 作用域 */ var foo = function ( ) { var a = 3, b = 5; var bar = function ( ) { var b = 7, c = 11; // At this point, a is 3, b is 7, and c is 11 a += b + c; // At this point, a is 21, b is 7, and c is 11 }; // At this point, a is 3, b is 5, and c is not defined bar( ); // At this point, a is 21, b is 5 }; /* 闭包 回调 */ /* 模块: 是一个提供接口却隐藏状态与现实的函数或对象 */

======================================================================================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值