变量作用域

自 ES2015 起,JS 引入letconst 关键词定义变量的块作用域(Block Scope)。

var 仅支持全局作用域(Global Scope)和函数作用域(Function Scope);

let 支持块作用域,即严格定义作用域在花括号{}里面;

counst 不仅支持块作用域,且定义的变量无法再修改。

  1. var i = 5;
  2. for (var i = 0; i < 10; i++) {
  3. // some statements
  4. }
  5. // Here i is 10
  1. let i = 5;
  2. for (let i = 0; i < 10; i++) {
  3. // some statements
  4. }
  5. // Here i is 5

[更多]


循环操作

for 循环

C 风格

  1. for (let i = 0; i < arr.length; i++) {
  2. // do iteration
  3. }

JS 风格

  1. for (let val of arr) {
  2. // loop over values of array
  3. }
  1. for (let key in obj) {
  2. // loop over keys of object
  3. }

对象(Object)

创建新对象的四种方式

Object Initializer

  1. let brother = {
  2. name: 'Alex',
  3. age: 25,
  4. sayHello: function () {
  5. return 'Hello!' + 'my name is' + this.name;
  6. }
  7. }

Constructor function

  1. function Person (name, age, gender) {
  2. this.name = name;
  3. this.gender = gender;
  4. this.age = age;
  5. sayHello: function () {
  6. return 'Hello!' + 'my name is' + this.name;
  7. }
  8. }
  9. let brother = new Person('Alex', 25, 'male');

Object Constructor

  1. let brother = new Object({
  2. name: 'Alex',
  3. gender: 'male'
  4. age: 25,
  5. sayHello: function () {
  6. return 'Hello!' + 'my name is' + this.name;
  7. }
  8. })

Object.create() method

  1. let brother = {
  2. name: 'Alex',
  3. gender: 'male'
  4. age: 25,
  5. sayHello: function () {
  6. return 'Hello!' + 'my name is' + this.name;
  7. }
  8. }
  9. let sister = Object.create(brother);
  10. sister.name = 'Alice';
  11. sister.gender = 'female'

补充:

sister = Object.create(brother) 在这里实际上是以 brother 作为原型构建的,亦即:

  1. sister.__proto__ === brother

将返回 true


对象原型(Object Prototype)

我们刚刚创建的brother 对象的 constructor 是 Person(),而 Person() 的原型是 Object()。如果我们运行:

  1. brother.valueOf();

将列出 brother 中所有的属性和方法。但是,Person() 中并没有 valueOf() 方法,因此可以看出实际上 Person() 实际上继承了 Object() 中的方法 valueOf()

访问对象原型

目前主流浏览器都支持:

  1. brother.__proto__;

自 ECMAScript 2015 开始,也可以:

  1. Object.getPrototypeOf(brother);

可继承成员

每个对象都有个属性 prototype, 例如 Object.prototypePerson.prototype

Object.prototype 本身是一个对象,里面是 Object 的所有可继承成员。换句话说,prototype 里面不包含的成员是无法继承的。

修改原型

增加属性

我们可以直接给对象 bother 添加属性

  1. brother.education = 'Bachelor of Computer Science';

也可以给对象原型 Person 添加属性

  1. Person.prototype.nationality = 'Chinese'

增加方法

我们可以直接给对象 bother 添加方法

  1. brother.coding = function(){
  2. return '0 warning(s), 0 error(s)';
  3. };

也可以给对象原型 Person 添加属性

  1. Person.prototype.play = function(){
  2. return 'Happy';
  3. };

Reference


原型继承(Prototype Inheritance)

现在,假设我们想从 Person 构建一个 Student 子类。

Constructor function

首先需要构造函数(constructor function):

  1. function Student(name, age, gender, degree) {
  2. Person.call(this, name, age, gender);
  3. this.degree = 'Undergraduate';
  4. }

Prototype & Constructor Reference

此时,构造函数 Student() 的原型属性(prototype property)Student.prototype 并不具有 Person 里带有的方法, 因此还需要继承 Person 的方法。

例如, sayHello()Person 的方法,却不在 Student.prototype 中。

修改原型

  1. Student.prototype = Object.create(Person.prototype)

Person.prototype 为原型构建对象并赋给 Student.prototype 就可以继承到 Person 的方法了。

恢复构造函数

但仅仅这样会造成另一个问题。由于 Student.prototype === Person.prototype,造成了 Student.prototype.constructor === Person.prototype.constructor。因此需要将 Student 的构造函数恢复成 Student 而不是 Person

  1. Student.prototype.constructor = Student

这样,继承就完成了。

Reference

Pipeline

  1. const pipe = (f1, f2) => {
  2. return (arg) => {
  3. const result1 = f1(arg);
  4. return f2(result1);
  5. }
  6. }
  7. let timesTwo = (a) => a*2;
  8. let timesThree = (a) => a*3;
  9. const pipeline = pipe(timesTwo, timesThree);
  10. console.log(`$6 x 2 x 3 = ${pipeline(6)}`);

Reference


JS String

字符串转数字

  1. Number(string);
  2. parseInt(string);
  3. parseFloat(string);

字符串截取

  1. string.charAt(index);
  2. string.indexOf("foo");
  3. string.lastIndexOf("foo")
  4. string.match("pattern");
  5. string.replace(/regex/, "foo");
  6. string.slice(start, end);
  7. string.substring(start, end);
  8. string.substr(start, length);

字符串转数字

  1. let number = Number(string);
  2. let intNum = parseInt(string);
  3. let floatNum = parseFloat(string);

Reference


Written with StackEdit.

Javascript 初学笔记的更多相关文章

  1. javascript初学笔记

    基本语句 赋值条件循环语句 javascript异常处理语句 trycatchfinally语句 Error对象 throw语句 函数 定义 调用 嵌套函数 函数的嵌套定义 内置函数 匿名函数和Fun ...

  2. Javascript初学篇章_5(对象)

    对象 Javascript是一种面向对象的语言,因此可以使用面向对象的思想来进行javascript程序设计对象就是由一些彼此相关的属性和方法集合在一起而构成的一个数据实体.举个例子,一只猫是个对象, ...

  3. C++ STL初学笔记

    C++  STL初学笔记 更系统的版本见徐本柱的PPT set 在这儿:http://www.cnblogs.com/pdev/p/4035020.html #include <vector&g ...

  4. JavaScript基础笔记二

    一.函数返回值1.什么是函数返回值    函数的执行结果2. 可以没有return // 没有return或者return后面为空则会返回undefined3.一个函数应该只返回一种类型的值 二.可变 ...

  5. JavaScript基础笔记一

    一.真假判断 真的:true.非零数字.非空字符串.非空对象 假的:false.数字零.空字符串.空对象.undefined 例: if(0){ alert(1) }else{ alert(2) } ...

  6. Java程序猿的JavaScript学习笔记(汇总文件夹)

    最终完结了,历时半个月. 内容包含: JavaScript面向对象特性分析,JavaScript高手必经之路. jQuery源代码级解析. jQuery EasyUI源代码级解析. Java程序猿的J ...

  7. javascript - 工作笔记 (事件四)

    在javascript - 工作笔记 (事件绑定二)篇中,我将事件的方法做了简单的包装,  JavaScript Code  12345   yx.bind(item, "click&quo ...

  8. Java程序猿的JavaScript学习笔记(8——jQuery选择器)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...

  9. Java程序猿JavaScript学习笔记(2——复制和继承财产)

    计划和完成在这个例子中,音符的以下序列: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaSc ...

随机推荐

  1. 字符型设备驱动程序-first-printf以及点亮LED灯(一)

    学习使用 Linux 的  字符型设备驱动 来 进行 . 学习地址:http://edu.51cto.com/lesson/id-25710.html 第一步: 首先写 三个函数 ,2017年5月17 ...

  2. Decorator(装饰)模式

    1. 概述 若你从事过面向对象开发,实现给一个类或对象增加行为,使用继承机制,这是所有面向对象语言的一个基本特性.如果已经存在的一个类缺少某些方法,或者须要给方法添加更多的功能(魅力),你也许会仅仅继 ...

  3. git私服

    目录 安装 git 在服务器上部署 Git 1.在服务器上创建一个新用户 2.创建一个git仓库 3.在服务器端打开RSA认证(重要) 4.在客户端创建SSH key 5.把步骤4生成的公钥导入服务器 ...

  4. oracle的sys和system的默认密码

    oracle的sys和system默认密码system默认:manager sys默认:change_on_install使用PL/SQL Plus登录数据库时,system用户使用密码manager ...

  5. App跳转系统设置界面

    NSString * urlString = @"App-Prefs:root=WIFI"; if ([[UIApplication sharedApplication] canO ...

  6. 关于iOS Block当中为什么要用weakSelf和strongSelf的思考

    场景:当你在某个界面请求网络数据的时候,用户不愿意等待点击了返回按钮,此时在Block当中用如下的方式使用weakSelf的话,有可能会奔溃(因为在并发编程的情况下,虽然在if判断的时候weaksel ...

  7. MySQL-5.5.32 配置文件优化详解

    目录 MySQL-5.5.32 配置文件优化详解 一.配置文件说明 2.my-medium.cnf 3.my-large.cnf 4.my-huge.cnf 5.my-innodb-heavy-4G. ...

  8. Ubuntu下配置jdk和tomcat

    一.配置jdk 更新系统 apt-get update 添加ppa add-apt-repository ppa:webupd8team/java 开始安装 apt-get install oracl ...

  9. js数组的处理使用

    var users = [ {name: "张含韵", "email": "zhang@email.com"}, {name: " ...

  10. HCNA(二)以太网的帧结构

    一.网络通讯协议 一般地,关注于逻辑数据关系的协议通常被称为上层协议,而关注于物理数据流的协议通常被称为低层协议. IEEE802就是一套用来管理物理数据流在局域网中传输的标准,包括在局域网中传输物理 ...