• LazyMan

    • 实现LazyMan(什么是LazyMan?请自行google)
    • function _LazyMan(_name) {
      var _this = this;
      _this.tasks = [];
      _this.tasks.push(function() {
      console.log('Hi! This is ' + _name + '!');
      // 这里的this是window,所以要缓存this
      _this.next();
      });
      setTimeout(function() {
      _this.next();
      }, 0);
      } // push函数里面的this和setTimeout函数里面的this都指向全局作用域,所以要缓存当前this指向
      _LazyMan.prototype.next = function() {
      var _fn = this.tasks.shift();
      _fn && _fn();
      }
      _LazyMan.prototype.sleep = function(_time) {
      var _this = this;
      _this.tasks.push(function() {
      setTimeout(function() {
      console.log('Wake up after ' + _time);
      _this.next();
      }, _time);
      });
      return _this;
      }
      _LazyMan.prototype.sleepFirst = function(_time) {
      var _this = this;
      _this.tasks.unshift(function() {
      setTimeout(function() {
      console.log('Wake up after ' + _time);
      _this.next();
      }, _time);
      });
      return _this;
      }
      _LazyMan.prototype.eat = function(_eat) {
      var _this = this;
      _this.tasks.push(function() {
      console.log('Eat ' + _eat);
      _this.next();
      });
      return _this;
      } // 封装对象
      var LazyMan = function(_name) {
      return new _LazyMan(_name);
      }
    • 数据类型

      • 6种原始值(不可变。“除非重置当前变量,否则不能改变元素值。”)
        1. Null(只有一个值: null)
        2. Undefined(一个没有被赋值的变量会有个默认值 undefined)
        3. Number
        4. Boolean(两个值:true 和 false)
        5. String
        6. Symbol
      • 和Object(对象指内存中的可以被标识符引用的一块区域)
    • 数据类型检测

      • typeof(对变量或值调用 typeof 运算符将返回(字符串)下列值之一)
        1. undefined - Undefined类型
        2. number - Number类型
        3. boolean - Boolean类型
        4. string - String类型
        5. symbol - Symbol类型(ECMAScript6新增)
        6. function - 函数对象([[Call]]在ECMA-262条款中实现了)
        7. object - 引用类型 或 Null类型
      typeof(Function) // function (Function是函数对象)
      typeof(new Function) // function (new Function也是是函数对象,同等:var func = function(){})
      typeof(Array) // function (Array是函数对象)
      typeof(new Array) // object(实例化的Array就是object)
    • 变量赋值时候的返回值:

      var name = 123; // 返回undefined
      name = 456; // 返回456

      结语:定义变量的时候赋值返回:undefined 给已声明变量赋值时候返回当前赋值。

    • 获取元素距离页面的top、left

      function getRec(ele) {
      var _t = document.documentElement.clientTop,
      _l = document.documentElement.clientLeft,
      rect = ele.getBoundingClientRect();
      return {
      top: rect.top - _t,
      right: rect.right - _l,
      bottom: rect.bottom - _t,
      left: rect.left - _l
      }
      }

      注意:IE、Firefox3+、Opera9.5、Chrome、Safari支持,在IE中,默认坐标从(2,2)开始计算,导致最终距离比其他浏览器多出两个像素,我们需要做个兼容。

    • 数字的固定小数位数

      var a=8.88888,
      b=8;
      console.log(a.toFixed(2)); // 8.89 或者 8.88
      console.log(b.toFixed(2)); // 8.00
    • js是编译语言,数组长度是随时程序变化而变化的

      var arr = [0, 1];
      arr[3] = 3;
      console.log(arr[2]); // undefined
      console.log(arr.length); // 4
    • 矩阵的转置

      var arr = [ // 定义一个矩阵(二维数据)
      [1, 2, 3, 4],
      [5, 6, 6, 6],
      [7, 6, 7, 8],
      [8, 5, 3, 3]
      ]; function changeArr(arr) { // 矩阵转置函数
      var c;
      for (var i = 1; i < arr.length; i++) {
      for (var j = 0; j < i; j++) {
      c = arr[i][j];
      arr[i][j] = arr[j][i];
      arr[j][i] = c;
      }
      }
      }
      changeArr(arr);
      console.table(arr);
    • 冒泡排序方法

      // 第一轮是对n-1的位置定位
      // 第二轮是 每一个位置的数的 确定
      var arr = [1, 4, 5, 6, 99, 111, 112, 113, 133],
      temp = 0,
      flag = false;
      for (var i = 0; i < arr.length - 1; i++) {
      document.writeln('come');
      for (var j = 0; j < arr.length - 1 - i; j++) {
      if (arr[j] > arr[j + 1]) {
      temp = arr[j];
      arr[j] = arr[j + 1];
      arr[j + 1] = temp;
      flag = true;
      }
      }
      if (flag) {
      flag = false;
      } else {
      break;
      }
      }
      for (var i = 0; i < arr.length; i++) {
      document.writeln(arr[i]);
      };
    • 二分查找

      var arr = [41, 55, 76, 87, 88, 99, 123, 432, 546, 577, 688, 786];
      
      function twoFind(arr, wantVal, leftIndex, rightIndex) {
      if (leftIndex > rightIndex) {
      document.writeln('SORRY: 找不到 ' + wantVal + ' !');
      return;
      }
      var minIndex = Math.floor((leftIndex + rightIndex) / 2);
      if (arr[minIndex] > wantVal) {
      twoFind(arr, wantVal, leftIndex, minIndex - 1);
      } else if (arr[minIndex] < wantVal) {
      twoFind(arr, wantVal, minIndex + 1, rightIndex);
      } else {
      document.writeln('找到了 ' + wantVal + ' ,下表为' + minIndex);
      }
      }
      twoFind(arr, 9, 0, arr.length - 1);
    • js对象访问属性的二种方式

      function Person () {};
      var new1 = new Person ();
      new1.name='冯杰';
      new1.age=21;
      window.alert(new1.name);
      window.alert(new1["age"]);
    • js之delete只能删除对象的属性

      function Person () {};
      var me = new Person();
      me.name='冯杰';
      console.log(me.name);
      delete me.name;
      console.log(me.name);
    • 在js中对象的方法不是通用的,如果生成n个对象,那么就有n个内存堆栈

      // js 中 一切类 继承自 Object 而Object 有propotype
      // 下面是解决办法 prototype 获得类的static性质
      function God() {}
      God.prototype.shout = function() {
      window.alert('小狗叫');
      }
      var dog1 = new God();
      var dog2 = new God();
      dog1.shout();
      dog2.shout();
    • 对象

      // js里要想创建对象 除了一般的创建方式 还有 通过Object 方式创建类
      // Object 类是所有js类的基类 Object 就表示对象(一切的对象)
      var p1 = new Object();
      p1.name = 'fj';
      window.alert(p1.name);
      window.alert(p1.constructor); // 原型链上新增默认对象方法
      var num = new Number(1);
      var num2 = 10;
      window.alert(num.constructor);
      window.alert(num2.constructor);
      // 上面2个弹出是一样的
      Number.prototype.add = function(a) { //prototype是属于类的
      return this + a;
      }
      window.alert(num.add(1).add(2)); // 小实验 为Array 添加 find(val) 方法
      Array.prototype.find = function(a) {
      for (var i = 0; i < this.length; i++) {
      if (this[i] == a) {
      return i;
      }
      }
      return 'find fail.';
      }
      var arr = [0, 1, 2, 77, 4, 5];
      window.alert(arr.find(77));
    • arguments

      function abc() {
      var sum = 0;
      for (var i = 0; i < arguments.length; i++) {
      sum += arguments[i];
      }
      return sum;
      }
      window.alert(abc(1, 2, 3));
    • call函数目的就是改变对象的this指向

      var Person = {
      name: 'fjj'
      }; function test() {
      window.alert(this.name);
      }
      test.call(Person);
    • 体会js的封装

      function Person() {
      var name = 'fj'; //私有
      this.age = 21; //共有
      }
      var p1 = new Person();
      window.alert(p1.name); //undefined
      window.alert(p1.age); //21
    • prototype的方法不能访问私有属性和方法

      function Person() {
      var name = 'fj'; //私有
      this.age = 21;
      }
      Person.prototype.showName = function() {
      window.alert(this.name);
      }
      Person.prototype.showAge = function() {
      window.alert(this.age);
      }
      var p1 = new Person();
      p1.showName();
      p1.showAge();
    • 继承

      // js 里面是对象冒充来继承的 不算是真正的继承 通过对象冒充 js可以实现多重继承和继承的效果 但是没有Extends关键字
      function Father(name, age) {
      this.name = name;
      this.age = age;
      this.show = function() {
      window.alert(this.name + '---' + this.age);
      }
      } function Son(name, age) {
      this.Father = Father;
      this.Father(name, age); //通过对象冒充 实现继承 这一句非常重要 js是动态语言 不是编译语言 要执行才会分配空间
      }
      var me = new Son('fj', 21);
      window.alert(me.name);
      me.show();
    • 重载

      // js从常理来说是不支持重载的 但是又可以说是天然支持重载 因为js天然支持可变参数 而且我们可以通过arguments[]数组的长度判断 而做出相应的处理
    • 闭包

      // 闭包实际上设计一个对象的属性,何时被gc处理的问题 闭包和gc是相关联的
    • 数组长度

      // 数组的长度是根据下标的最大而确定的
      var arr = new Array();
      arr['a'] = 1;
      arr['b'] = 2;
      window.alert(arr.length); // 打出0

Javascript重点汇总的更多相关文章

  1. JavaScript 重点笔记

    JavaScript 重点笔记 ## 数组 // 必须掌握 - arr.length:获取数组元素的长度 - arr.splice(起始位置,长度):从数组中添加或删除元素. - arr.indexO ...

  2. AST抽象语法树——最基础的javascript重点知识,99%的人根本不了解

    AST抽象语法树——最基础的javascript重点知识,99%的人根本不了解 javascriptvue-clicommonjswebpackast  阅读约 27 分钟 抽象语法树(AST),是一 ...

  3. 学习SVG 重点汇总

    什么是SVG? Δ  SVG 指可伸缩矢量图形 (Scalable Vector Graphics) Δ  SVG 用来定义用于网络的基于矢量的图形 Δ  SVG使用XML格式来定义图形 Δ  SVG ...

  4. JavaScript要点汇总——The Most Important

    关于JavaScript的基础变量,运算符的详解以及基本的分支循环嵌套已经在 JS基础变量及JS中的运算符 JS中的循环分支嵌套 说过了,今天我们所说的是做网页中最长用到的东西.内容不算少,要有耐心, ...

  5. javascript树形汇总金额

    在开发企业应用的时候总会遇到树形汇总金额的场景,即将树形的列表中的叶子节点(没有子节点)的金额汇总到父节点上. 这种需求一般是在前端进行处理,即使用JavaScript处理,因为叶子节点的金额可能是不 ...

  6. JavaScript学习汇总

    对于JavaScript,还是无法割舍,有心无力,时间总是匆匆,暂且都放在这里吧 javascript中this的使用 写的很不错的一偏文章,简单看了下,mark了吧 原文:http://davids ...

  7. javascript算法汇总(持续更新中)

    1. 线性查找 <!doctype html> <html lang="en"> <head> <meta charset="U ...

  8. javascript重点笔记

    操作符之间的优先级(高到低):算术操作符 >比较操作符 >逻辑操作符 >"="赋值符号 算术运算符

  9. JavaScript操作符汇总

    操作符 JavaScript 有赋值.比较.算术.位.逻辑.字符串和特殊运算符.本章描述了操作符,以及关于操作符优先级的一些信息. 表 2.1 JavaScript 所有操作符简明列表. 表 2.1 ...

随机推荐

  1. 《算法》第六章部分程序 part 3

    ▶ 书中第六章部分程序,包括在加上自己补充的代码,后缀树的两种实现 ● 后缀树实现一 package package01; import java.util.Arrays; import edu.pr ...

  2. jquery源码'jQuery.fn.init.prototype'

    一般我们在创建构造函数即使用的时候会这样写,使用的时候会使用new 关键字,先实例化,然后使用. function test(name, age) { this.name = name; this.a ...

  3. gevent mysql

    使用gevent实现mysql并发时,每个greenlet应该独享一个mysql连接,否则,不同的greenlet之间会相互影响. ultramysql doesn't allow you to ma ...

  4. CSS TYPOGRAPHY

    CSS TYPOGRAPHY Review Great job! You learned how to style an important aspect of the user experience ...

  5. Firebird日期时间操作

    最近在使用Firebird数据做 一项目,使用FireBird边用边学.(以下转贴) 查询2007年度以后的,12月份以上的数据记录,datetime为timestamp字段 select * fro ...

  6. PostgresQL 中有没有rownum这样的,显示结果集的序号

    select * from (select row_number() over() as rownum,tablename from pg_tables) t where rownum<10;

  7. Pronunciation Guide for 25 Common Fruits

    Pronunciation Guide for 25 Common Fruits Share Tweet Share Tagged With: Vocabulary Words Know how to ...

  8. svn的上传冲突问题

    上传报错实际是 1 . 之前上传的代码与现代码不一样 2. 上传的代码中有错误 需要先拉下来,对比删除不要的,再上传 eclipse加入svn :  1.import  ---从svn检出项目---创 ...

  9. Nmap结果文件XML文件解析

    对nmap扫描结果xml格式的文件进行解析,无需直接xml解析或读取,可直接使用模块: 1.nmapparser 安装:pip install nmapparser Demo: #!/usr/bin/ ...

  10. lucene solr

    理解 lucene 是一个全文搜索的引擎 solr是全文搜索的web实现 --------------------.  java.lang.UnsupportedClassVersionError:  ...