javaScript数据结构与算法

avatarplhDigital nomad

javaScript 面向对象

老方法


function Checking(amount) {
  this.balance = amount;
  this.deposit = deposit;
  this.withdraw = withdraw;
  this.toString = toString;
}

// 存amount的钱
function deposit(amount) {
  this.balance += amount;
}

// 花掉
function withdraw(amount) {
  if (amount <= this.balance) {
    // 够钱花就减去
    this.balance -= amount;
  } else if (amount > this.balance) {
    // 不够钱就打印不够钱
    console.log('钱不够花啊');
  }
}

// 展示还有多少钱
function toString() {
  return `Balance: ${this.balance}`;
}

// 新建一个钱包
const account = new Checking(500);
account.deposit(1000);
console.log(account.toString());
account.withdraw(1501);
console.log(account.toString());

新的es6这样新建一个对象

class Checking {
  constructor(amount) {
    this.balance = amount;
  }
  deposit(amount) {
    this.balance += amount;
  }
  withdraw(amount) {
    if (this.balance < amount) {
      console.log(`你只有${this.balance},不够花啊`);
    } else {
      console.log(`你只有${this.balance},不够花啊`);
      this.balance -= amount;
    }
  }
  toString() {
    return `余额:${this.balance}`;
  }
}
// // 新建一个钱包
const account = new Checking(500);

account.deposit(1000);
console.log(account.toString());
account.withdraw(750);
console.log(account.toString());
account.withdraw(850);
console.log(account.toString());

计算平均数

class WeekTemps {
  constructor() {
    this.dataStore = [];
  }
  add(temp) {
    this.dataStore.push(temp);
  }
  average() {
    let total = 0;
    for (let i = 0; i < this.dataStore.length; i += 1) {
      total += this.dataStore[i];
    }
    return total / this.dataStore.length;
  }
}
const thisWeek = new WeekTemps();
thisWeek.add(2);
thisWeek.add(2);
thisWeek.add(22);
thisWeek.add(2);

console.log(thisWeek.average());

创建一个可展示学生成绩,添加成绩,求成绩平均数的对象


class Student {
  constructor() {
    // constructor 中的数组可用于储存// 类似于私有变量
    this.arr = [];
  }
  // 记录学生成绩
  show() {
    return this.arr;
  }
  // 添加学生成绩
  add(achievement) {
    this.arr.push(achievement);
  }
  average() {
    const all = this.arr.reduce((a, b) => a + b);
    return all / this.arr.length;
  }
}

const student = new Student();
student.add(94);
student.add(93);
console.log(student.show());
console.log(student.average());
student.add(93);
console.log(student.show());
console.log(student.average());

将一组单词存在数组中,提供正向,反向排序的方法;


class ShowArr {
  constructor(arr) {
    // props 就是class在创建时候传入的参数;
    this.arr = arr;
  }
  sort() {
    return this.arr.sort((a, b) => (a - b > 0));
  }
  reverse() {
    return this.arr.sort((a, b) => (a - b > 0)).reverse();
  }
}
const showArr = new ShowArr([1, 2, 3, 4, 5, 66, 22, 34]);
console.log(showArr.sort());
console.log(showArr.reverse());

如何判断对象是否拥有属性(不来自于继承)

showArr.hasOwnProperty('sort')    // true => 自有属性   // false 属性来自于继承&&根本没有这个属性
showArr instanceof ShowArr         // true   => 属性来自于继承
showArr instanceof Object            // true   =>  原型链继承   showArr => ShowArr => Object

对象的属性和方法的不同(其实没什么不同,只是创建的方法不一致)

class ShowArr {
  constructor(arr) {
    this.arr = arr;
  }
  sort() {
    return this.arr.sort((a, b) => (a - b > 0));
  }
}
ShowArr.prototype.propAdd = function () {
  return 'proptest';
};
const showArr = new ShowArr([1, 2, 3, 4, 5, 66]);
showArr.test = 'test';
console.log(showArr);

image

结论

在用原型继承编写复杂代码之前,了解原型继承模型非常重要。同时,要注意代码中的原型链的长度,并在必要时将其分解,以避免潜在的性能问题。此外,永远不要扩展原生对象的原型,除非是为了兼容新的JavaScript特性。

BST二叉树基础

先新建一个节点类,将以将这个想像成超级简化版的dom树,每个节点最多2个子节点 节点拥有信息,data储存数据,储存子节点this.left < this.data < this.right,遵从这样的规律

class Node {
  constructor({ data, left, right }) {
    this.data = data;
    this.left = left || null;
    this.right = right || null;
  }
  show() {
    return this.data;
  }
}

下面新建BST类

class BST {
  constructor() {
    this.root = null;
  }
  insert({ data }) {
    const n = new Node({ data });
    if (this.root === null) {
      this.root = n;
    } else {
      let current = this.root;
      let parent;
      while (true) {
        parent = current;
        if (data < current.data) {
          current = current.left;
          if (current == null) {
            parent.left = n;
            break;
          }
        } else {
          current = current.right;
          if (current == null) {
            parent.right = n;
            break;
          }
        }
      }
    }
  }
  inOrder(node) {
    if (!(node == null)) {
      this.inOrder(node.left);
      console.log(`${node.show()} `);
      this.inOrder(node.right);
    }
  }
}

以上给他写了插入的方法,通过下述代码描述

const nums = new BST();
nums.insert({ data: 23 });
nums.insert({ data: 45 });
nums.insert({ data: 16 });
nums.insert({ data: 37 });
nums.insert({ data: 37 });
nums.insert({ data: 3 });
nums.insert({ data: 99 });
nums.insert({ data: 22 });
nums.inOrder(nums.root);
console.log(nums.root);

image