1,2,3基础知识,数组,列表

1,2,3基础知识,数组,列表

1.1 JavaScript环境

本小节讲述对JS的环境安装和使用

1.2 JavaScript 编程实践

本小节展示了js基础语法:定义、循环、判断、作用域等

1.3 对象和面向编程对象

以一个对象的代码为例介绍了js进行对象和面向对象编程

function account(amount) {    this.balance = amount    this.deposit = deposit    this.toString = toString    this.withdraw = withdraw}function deposit(deposit) {    this.balance += deposit}function toString() {    return `balance: ${this.balance}`}function withdraw(number) {    if(this.balance > number) {        this.balance -= number    } else {        retrun `余额不足`    }}# 列表# 列表的抽象定义+ listSize(属性): 列表元素的个数+ pos(属性): 列表当前的位置 + length(属性): 返回列表中元素的个数+ clear(方法) :清空list+ toString(方法): 返回字符串形式+ getElement(方法): 返回当前的位置+ insert(方法): 在现有的元素前插入+ append(方法): 在现有的元素后插入+ remove(方法): 删除列表的元素+ front(方法): 将列表的当前位置移动到第一个元素+ end(方法): 将列表的当前位置移动到最后一个元素+ next(方法): 将当前元素移动到后一位+ prev(方法): 将当前元素移动到前一位+ hasNext(方法): 判断下一位+ hasPrev(方法): 判断前一位+ moveTo(方法): 将当前位置移动到指定位置+ currPos(方法): 返回列表的当前位置

function List() {this.pos = 0this.dataStore = []}List.prototype = {length: function() {return this.dataStore.length;},clear: function() {this.dataStore = []},toString: function() {return this.dataStore},getElement: function() {return this.dataStore[this.pos]},find: function(element) {for (let i = 0; i < this.dataStore.length; i++) {if(element == this.dataStore[i]) {return i}return -1}},insert: function(element, target) {let targetIndex = this.dataStore.indexOf(target)if(targetIndex > -1) {this.dataStore.splice(targetIndex, 0, element)} else {return false}},append: function(element) {this.dataStore.push(element)},remove: function(element) {let index = this.dataStore.indexOf(element)if(index > -1) {this.dataStore.splice(index,1)} else {return false}},front: function() {this.pos = 0},end: function() {this.pos = this.dataStore.length -1},next: function() {if(this.pos < this.dataStore.length -1) {this.pos ++}},prev: function() {--this.pos},hasNext: function() {

    },    hasPrev: function() {},    moveTo: function(position) {        this.pos = position    },    contain: function(element) {        for (let index = 0; index < this.dataStore.length; index++) {            var item = this.dataStore[index];            if(item == element) {                return true            }        }        return false    }}let names = new List()names.append('Sacit')names.append('Leblonc')names.append('Sett')

2.1 数组

标准定义: 一个存储元素的线性集合,元素可以通过索引来任意取值

在JavaScript中这些索引都被转化成了字符串类型,因为Javascrpit的属性名必须是字符串类型

Javascript的数组严格意义上来说就是一个对象,也因为Array被视为对象,在使用中可以直接在编程里使用他的属性和方法

2.2 使用数组

2.2.1 创建数组

var numbers = []// numbers.lengthvar numners = new Array()var numners = new Array(1,2,3,4,5)var numners = new Array(10) // 只有一个参数时表示为数组的长度Array.isArray 判断是否为数组类型

2.2.2 数组读写

numbers[0] 读取numbers[1] = 'array' 写入

2.2.3 由字符串生成数组

str.split()

2.2.4 对数组的整体性操作

浅拷贝

被赋值的数组创建了新的引用var numbers = [0,1,2,3]var same = numbersnumbers[0] = 9same[0]的值也是9

2.3 存取函数

2.3.1 查找

arr. indexof

2.3.2 数组的字符串表示

arr. join toString

2.3.3 拼接

arr .concat()

2.4 可变函数

  • push 插尾
  • unshift 插头
  • pop 删尾
  • shift 删头
  • splice('起始索引', 删除个数, 想要添加进来的数组)
  • sort reverse 排序

2.5 迭代器

  • foreach
  • every
  • reduce
  • map
  • filter

2.6 二维数组和多维数组

2.6.1 创建二位数组

一维数组的每一项都是数组

var twod = []var rows = 5for(var i = 0; i< rows; i++) {    two[i] = []}Array.matrix = function(rows, columns, initial) {    var arr = []    for(var i = 0; i< rows; i++) {        arr[i] = []        for(var j = 0; j < columns, j++) {            arr[i][j] = initial        }    }    return arr}

2.6.2 处理二位数组

var nums = [[10,20,30], [100,200,300], [1,2,3]]var total nums.forEach(item => {    item.forEach(i => {        console.log(i)        total += i    })    console.log(total/ item.length, total, item.length)    total = 0})

2.6.3 参差不齐的数组

var grades = [[88,90],[90,95,88],[90,91,91,92]]var total = 0for(var i = 0; i< grades.length; i++) {    var col = grades[i]    for(var j = 0; j< col.length; j++) {        total += col[j]    }    console.log(total, total/ col.length)    total = 0}

2.7 对象数组

练习代码

function account(amount) {        this.balance = amount        this.deposit = deposit        this.toString = toString        this.withdraw = withdraw    }    function deposit(deposit) {        this.balance += deposit    }    function toString() {        return `balance: ${this.balance}`    }    function withdraw(number) {        if (this.balance > number) {            this.balance -= number        } else {            return `余额不足`        }    }    let myaccount = new account(1000)    // myaccount.deposit(500)    // myaccount.toString()    // myaccount.withdraw(800)    // myaccount.toString()    // myaccount.withdraw(1000)    // myaccount.toString()    // function grades(arr) {    //     this.grades = arr    //     this.add = addgrades    //     this.getAverage = getAverage    // }    // function addgrades(grades) {    //     this.grades.push(grades)    // }    // function getAverage() {    //     var total = 0    //     this.grades.forEach(element => {    //         total += element    //     });    //     return total / this.grades.length    // }    // let eg = new grades([10,20])    // eg.add(20)    // 'hello'.split('').reverse().join('')    function List() {        this.pos = 0        this.dataStore = []    }    List.prototype = {        length: function() {            return this.dataStore.length;        },        clear: function() {            this.dataStore = []        },        toString: function() {            return this.dataStore        },        getElement: function() {            return this.dataStore[this.pos]         },        find: function(element) {            for (let i = 0; i < this.dataStore.length; i++) {                if(element == this.dataStore[i]) {                    return i                }                return -1            }        },        insert: function(element, target) {            let targetIndex = this.dataStore.indexOf(target)            if(targetIndex > -1) {                this.dataStore.splice(targetIndex, 0, element)            } else {                return false            }        },        append: function(element) {            this.dataStore.push(element)        },        remove: function(element) {            let index = this.dataStore.indexOf(element)            if(index > -1) {                this.dataStore.splice(index,1)            } else {                return false            }        },        front: function() {            this.pos = 0        },        end: function() {            this.pos = this.dataStore.length -1        },        next: function() {            if(this.pos < this.dataStore.length -1) {                this.pos ++            }        },        prev: function() {            --this.pos        },        hasNext: function() {        },        hasPrev: function() {},        moveTo: function(position) {            this.pos = position        },        contain: function(element) {            for (let index = 0; index < this.dataStore.length; index++) {                var item = this.dataStore[index];                if(item == element) {                    return true                }            }            return false        }    }    let names = new List()    names.append('Sacit')    names.append('Leblonc')    names.append('Sett')    
免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部