复制代码

javascript

let const

\(let\) 局部声明

$const $ 全局声明

String Number Boolean null undefined

字符串,数字,布尔值,空值,未定义

console.log(typeof A);  // A 的类型

连接 & 模板字符串

const username = "dzk";
const age = 20;
const hello = `My name is ${username} and I am ${age}`;
console.log(hello);
console.log("My name is " + username + " and I am " + age);

字符串函数

const s = "Hello World!";
const A = "dzk,like,kan,p";
console.log(s.length); // 12
console.log(s.toUpperCase()); // HELLO WORLD!
console.log(s.toLowerCase()); // hello world!
console.log(s.substring(0, 5));// Hello
console.log(s.split(" "));// [ 'Hello', 'World!' ]
console.log(A.split(","));// [ 'dzk', 'like', 'kan', 'p' ]

数组

const fruit = ["apple", "banana", "cherry"];
fruit.push("orange");
console.log(fruit);
console.log(Array.isArray(fruit));// 判断是不是数组
console.log(fruit[0]);
console.log(fruit.indexOf("banana"));// 查询位置
fruit.unshift("kiwi");//前插入
fruit.pop();//出
console.log(fruit);

对象

const Du_zk = {
    age : 20,
    Name : "duzhongkun",
    gender : "male",
    height : 1.76,
    weight : 6,
    bmi : function(){
        return this.weight / (this.height * this.height);
    },
    isFat : function(){
        return this.bmi() > 25;
    },
    hobbies : ["watching", "reading", "table tennis"],
    address : {
        province: "shandong",
        city: "linyi",
    },
};
console.log(Du_zk.bmi());
console.log(Du_zk.isFat());
console.log(Du_zk.hobbies[1]);
console.log(Du_zk.address.city);
Du_zk.email = "duzhongkun@163.com";
console.log(Du_zk.email);

对象数组

const todos = [
    {
        id: 1,
        text: 'Take out trash',
        isCompleted: false,
    },
    {
        id: 2,
        text: 'Meeting with boss',
        isCompleted: true,
    },
    {
        id: 3,
        text: 'Dentist appointment',
        isCompleted: false,
    },
];

console.log(todos[1].text); // Meeting with boss
const todoJSON = JSON.stringify(todos);// 转化
console.log(todoJSON);
//[{"id":1,"text":"Take out trash","isCompleted":false},{"id":2,"text":"Meeting with boss","isCompleted":true},{"id":3,"text":"Dentist appointment","isCompleted":false}]

JSON

JSON:是一种轻量级的数据交换格式,使用文本表示结构化数据,通常是字符串格式。

JSON:常用于数据传输,如在前后端通信中,API返回的数据通常是JSON格式。

JSON格式通常更易于人类阅读,结构清晰。JSON可以被解析为对象数组。

JSON的数据类型包括字符串、数字、布尔值、数组和对象,不能包含函数或未定义的值。

对象数组:在编程语言中,尤其是JavaScript,表示一组对象的集合,通常是数组的形式。

对象数组:用于存储和操作数据,适合在代码中进行逻辑处理和迭代。

对象数组是编程语言中直接可用的结构,具有更强的可操作性。

对象数组可以包含方法和其他复杂的数据结构。

Free Online JSON Formatter - FreeFormatter.com 形式转换平台

If

//x is 10
//y is not 10
//y is 10

const x = 10;
if(x === 10) {
    console.log("x is 10");
}

const y = "10";
if(y === 10) {
    console.log("y is 10");
}
else{
    console.log("y is not 10");
}

if(y == 10) {
    console.log("y is 10");
}
else{
    console.log("y is not 10");
}

switch

const x = 10;
const color = x > 10 ? "blue" : "red";
switch (color) {
  case "blue":
    console.log("The color is blue");
    break;
  case "red":
    console.log("The color is red");
    break;
  default:
    console.log("The color is neither blue nor red");
};

for while

const Du_zk = {
    age : 20,
    Name : "duzhongkun",
    gender : "male",
    height : 1.76,
    weight : 6,
    bmi : function(){
        return this.weight / (this.height * this.height);
    },
    isFat : function(){
        return this.bmi() > 25;
    },
    hobbies : ["watching", "reading", "table tennis"],
    address : {
        province: "shandong",
        city: "linyi",
    },
};
for(let key in Du_zk){
    console.log(key, Du_zk[key]);
}
let keys = Object.keys(Du_zk);
while(keys.length){
    let key = keys.pop();
    console.log(key, Du_zk[key]);
}
let keyss = Object.keys(Du_zk);
for(let i = 0; i < keyss.length; i++){
    console.log(keyss[i], Du_zk[keyss[i]]);
}

posted @ 2024-09-25 22:51  Elgina  阅读(12)  评论(1)    收藏  举报