JavaScript

编写 $javascript$ 。

1
2
3
4
5
6
7
8
<script type="text/javascript">
// 浏览器弹窗
alert("tips");
// 向 body 输出
document.writeln("something")
// 向控制台输出
console.log("It is console")
</script>

运行外部文件(引入 $src$ 后 $script$ 中内容就无效)。

1
<script type="text/javascript" src="extern.js"></script>

Variable

声明

1
2
var a = 10
var name = value

typeof

输出变量类型。

1
2
3
4
var a = 10
console.log(typeof a)
a = "sedrt"
console.log(typeof a)

变量类型

1
2
3
4
5
6
number
boolean
string
null
undefined
object // 引用数据类型

Date

方法 描述
getDate() 以数值返回天(1-31)
getDay() 以数值获取周名(0-6)
getFullYear() 获取四位的年(yyyy)
getHours() 获取小时(0-23)
getMilliseconds() 获取毫秒(0-999)
getMinutes() 获取分(0-59)
getMonth() 获取月(0-11)
getSeconds() 获取秒(0-59)
getTime() 获取时间(从 1970 年 1 月 1 日至今)

String

方法 描述
charAt() 返回指定位置处的字符。
charCodeAt() 返回指定位置处字符编码。
codePointAt() 返回字符串中索引(位置)处的 Unicode 值。
concat() 返回两个或多个连接的字符串。
constructor 返回字符串的构造函数。
endsWith() 返回字符串是否以指定值结尾。
fromCharCode() 将 Unicode 值作为字符返回。
includes() 返回字符串是否包含指定值。
indexOf() 返回值在字符串中第一次出现的位置。
lastIndexOf() 返回值在字符串中最后一次出现的位置。
length 返回字符串中的字符数。
localeCompare() 使用基于本地的顺序来比较字符串。
match() 在字符串中搜索值或正则表达式,并返回匹配项。
prototype 允许您向对象添加属性和方法。
repeat() 返回拥有多个字符串副本的新字符串。
replace() 在字符串中搜索值或正则表达式,并返回替换值的字符串。
search() 检索字符串中与正则表达式匹配的子串。
slice() 提取字符串的一部分并返回新字符串。
split() 将字符串拆分为子字符串数组。
startsWith() 检查字符串是否以指定字符开头。
substr() 从字符串中抽取子串,该方法是 substring() 的变种。
substring() 从字符串中抽取子串。
toLocaleLowerCase() 使用主机的语言环境返回转换为小写字母的字符串。
toLocaleUpperCase() 使用主机的语言环境返回转换为大写字母的字符串。
toLowerCase() 返回转换为小写字母的字符串。
toString() 将字符串或字符串对象作为字符串返回。
toUpperCase() 返回转换为大写字母的字符串。
trim() 返回删除了空格的字符串。
trimEnd() 返回从末尾删除空格的字符串。
trimStart() 返回从开头删除空格的字符串。
valueOf() 返回字符串或字符串对象的原始值。

类型转换

$toString()$

1
console.log(a.toString()) // number -> string

$String()$ $null, undefined$ 可以使用。

1
console.log(String(a))

$Number()$

1
2
var b = "123"
var c = Number(b)

$parseInt(), parseFloat()$

1
parseInt(str, base)

$Boolean()$ (数字 $0, NaN$ ,字符串空串,$null, undefined$ 转为 $false$)

运算符

基础运算符会类型自动转换。

$===$ 不类型转换比较,类型不一样 $false$,类型一样且值一样 $true$

1
console.log(a===b)

$!==$ 与 $===$ 相反。

Object

声明 object 并且添加属性。

obj.dataname = value

1
2
3
4
var obj = new Object()
obj.username = "alice" // 两种均可
obj["name"] = "bob"
obj.gender = "f"

如果读取不存在的属性,会返回 undefined

属性可以是函数。

删除对象的属性。

delete obj.dataneme

1
delete obj.gender

in

判断对象中是否有某个属性。

1
2
3
if ("name" in obj) {
console.log("name in obj")
}

使用 ${ }$ 来创建对象。

1
var OBJ = { name: "a", gender: "f", age: 18}

function

1
2
3
4
5
function cout(str) {
console.log(str)
}

cout("s")

匿名函数

1
2
3
4
5
var f = function(str) {
console.log(str)
}

f("s")

对象

构造函数

1
2
3
4
5
6
7
8
function User(username, password) {
this.username = username
this.password = password
this.func = function() {
alert(this.username)
}
}
var user = new User("alice", "123456") // 必须有 new

instanceof 判断对象是否是某个类的实例。

1
2
3
var obj = new Object()
console.log(obj instanceof User)
console.log(user instanceof User)

prototype 该类的公共属性,该类的对象都可访问。

1
2
3
4
5
6
7
8
9
10
11
function User(username, password) {
this.username = username
this.password = password
}

User.prototype.type = "user"
var user1 = new User("alice", "123")
var user2 = new User("bob", "12345")

console.log(user1.type)
console.log(user2.type)

attribute in object

prototype 中含有某个属性时, in 会是 true 。

判断对象自身耳不判断原型中有无某个属性用 hasOwnProperty()

1
2
3
4
console.log("type" in user1) // true
console.log("username" in user1) //true
console.log(user1.hasOwnProperty("type")) //false
console.log(user1.hasOwnProperty("username")) // true

Array

1
2
3
4
var arr = new Array();
arr[9] = 1
console.log(arr[9]) // 1
console.log(arr[8]) // undefined

length 返回最大索引 + 1

1
console.log(arr.length) // 10

修改 length

增大,多出的部分空出来。

改小,多出的元素被删除。

字面量创建数组。

var name = []

1
2
3
var arr = [1, 2, 3, 4]
console.log(arr[3])
console.log(arr)

call apply

call(this: Function, thisArg: any, ...argArray: any[]): any

第一个参数影响函数内的 this,后面的参数是函数的参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var f = function(str, num) {
console.log(typeof this)
console.log(typeof str, str)
console.log(typeof num, num)
}

function User(username, age) {
this.username = username
this.age = age
}

var user = new User("scut", 1919)

f.call(user, "scut", 1919)

apply(this: Function, thisArg: any, argArray?: any): any

call 类似,函数参数要用数组传递。

1
f.apply(user, ["scut", 1919])