js语言记录

// hello world of js
console.log('Hello World');

/* 定义变量 */
// var, let, cont
// var 全局变量
// let 局部变量
// const 常量

/* 数据类型 */
// String, Number, Boolean, null, undefined
// null与undefine的区别:null被定义了,但为空;undefined表示未被定义

const age = 30;
// 可以通过  typeof 获取数据类型
console.log(typeof age);

let a;
console.log(typeof a);
a = 4;
console.log(typeof a);

/* 字符串 */
// 类似于c++,可以直接使用 + 拼接
console.log("My age is " + age + " years");
// 或者使用模版字符串
let str = 'My age is ${age} years';
console.log(str);

/* 字符串方法 */
// 获取长度
str = "Hello World";
console.log(str.length)
console.log(str.toUpperCase())
console.log(str.toLowerCase())
console.log(str.substring(0, 5))
console.log(str.split(""))

/* 数组 */
// 通过构造函数创建
const nums = new Array(1, 2, 3, 4, 5)
console.log(nums)
// 直接构建
const fruits = ['apples', 'oranges', 'pears', 10, true]
console.log(fruits);
console.log(fruits[3]);
fruits.push('mangos'); // 尾部插入
fruits.unshift('strawberries'); // 头部插入
console.log(fruits);

fruits.pop(); // 尾部删除

console.log(Array.isArray(fruits)); // 判断是否是一个数组

console.log(fruits.indexOf('oranges')); // 获取索引

/* 对象 */

const person = {
    firstName: "John",
    lastName: "Doe",
    age: 30,
    hobbies: ['music', 'movies', 'sports'],
};

console.log(person.hobbies)

// 解构
const {firstName, lastName, hobbies} = person;

console.log(firstName)

// 为对象增加新的属性
person.email = 'abc@qq.com';
console.log(person);
console.log(JSON.stringify(person)) // 将对象转换为json


/* if 条件语句 */
 
let apollo = 9;

// 双等号不考虑数据类型,即“9” == 9,也认为是true
// 三等号考虑数据类型
if (apollo === 9) {
    console.log("apollo is 9");
} else if(apollo > 9) {
    console.log("apollo is gt 9")
} else {
    console.log("apollo is not 9");
}

apollo = 4
if (apollo > 5 && apollo < 9) {
    console.log(" > 5 and < 9");
} else if(apollo <= 5 || apollo >= 9) {
    console.log(" <= 5 || >= 9");
}

// 三目运算符,和c++一样

// switch和c++一样

// for 和 while循环和c++一样

// 还有一种类似于c++ for(auto item : nums)的写法
for (let item of nums) {
    console.log(item)
}

 

js函数

普通写法

function fn (callback) {
    console.log('do something...');  
}

 

匿名函数的自运行:没有函数名,函数创建出来会立刻执行,和直接写代码没有区别

好处:变量x在函数的封闭空间,不会对外部造成命名污染

小括号的存在将函数的声明,变成了待执行的表达式

(function (callback) {
    let x = 100;
console.log(x);
})()

和以下相同

+function() { ... } ()
-function() { ... } ()
~function() { ... } ()
void function() { ... } ()
delete function() { ... } ()

 

回调函数

如果getToken是一个异步函数,但是想在getToken调用结束后,再调用其他函数,

因此可以在参数中引入回调函数,在getToken调用时将想要在其之后调用的函数传入,

并在getToken结束时执行

function getToken(callback){
  // 异步函数
  callback();
}

 

构造函数

// 构造函数代码

function fn() {
    ....// your code
}

let obj = new fn();

 

// 普通函数代码

function fn() {
    ....// your code
}

let res = fn();

目前 js也支持class关键字

过去的写法

function User (name, age) {
    this.name = name;
    this.age = age;  
}
User.prototype.login = function(){
    //....
}

let user = User();
user.login();

现在的写法

class User {
    constructor(name, age){
         this.name = name;
         this.age = age; 

    }  
    login(){
        //....
    }
}

let user = new User();
user.login();

 

闭包函数

函数b称之为闭包函数,通过这样的写法,c就等于函数b,打破了b只能在a内部使用的限制,且c可以访问a的局部变量

function a() {
    let x = 1;
    return function b() {
        console.log(x);
    }
}

let c = a();
c();

函数的柯里化

如果其中几个参数总是固定变化的,为了简便,可以用以下写法

// 该写法中x、y总是固定的

function fn(x) {
    return function (y) {
        return function (z) {
            //....
        }
    }
}

调用

let fab = fn('a')('b')
fab(参数)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Hello World</h1>
    <script src="index.js"></script>
</body>
</html>

 

posted @ 2023-12-19 15:34  WTSRUVF  阅读(2)  评论(0编辑  收藏  举报