GoogleHack―利用谷歌语法查找网站
743 2023-04-03 02:37:32
本小节讲述对JS的环境安装和使用
本小节展示了js基础语法:定义、循环、判断、作用域等
以一个对象的代码为例介绍了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')
标准定义: 一个存储元素的线性集合,元素可以通过索引来任意取值
在JavaScript中这些索引都被转化成了字符串类型,因为Javascrpit的属性名必须是字符串类型
Javascript的数组严格意义上来说就是一个对象,也因为Array被视为对象,在使用中可以直接在编程里使用他的属性和方法
var numbers = []// numbers.lengthvar numners = new Array()var numners = new Array(1,2,3,4,5)var numners = new Array(10) // 只有一个参数时表示为数组的长度Array.isArray 判断是否为数组类型
numbers[0] 读取numbers[1] = 'array' 写入
str.split()
浅拷贝
被赋值的数组创建了新的引用var numbers = [0,1,2,3]var same = numbersnumbers[0] = 9same[0]的值也是9
arr. indexof
arr. join toString
arr .concat()
一维数组的每一项都是数组
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}
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})
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}
练习代码
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')