JavaScript

JavaScript

在浏览器里运行,由Node(C++编写)来执行。

ECMAScript是一种规范,而JavaScript是符合该规范的编程语言

搭建开发环境

VSC+JavaScript+Live Server

JavaScript

简介

虽然<script>​标签可以写在<head>​和<body>​标签里,但是最建议的地方时<body>​标签的末尾。

因为你可能有很多<script>​标签,而浏览器是从上到下执行代码的,如果把<script>​标签放前面,那浏览器可能忙于解析<script>​代码,而不能加载页面。这个时候用户看到的web​页面就是白色的,会造成糟糕的用户体验。而且把<script>​标签放在后面,它可以操纵更多元素。

当然也有例外,比如有些第三方代码,需要放置在<head>​标签里

JS代码以;​结尾,//​为注释。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <h1>Hello World</h1>
  <script>
    //This is my first JS code!
    console.log('Hello World');
  </script>
</body>
</html>

JS代码分离

index.html​文件内容如下:

<script src="index.js"></script>​来引用JS​文件

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

index.js​文件内容如下

//This is my first JS code!
console.log('Hello World');

Node中运行JS

# 运行js文件
node index.js

JS中基础代码编写

// 变量声明
let name = 'Moss';
console.log(name);

// 常量声明
const interestRate = 1;

数据类型

基本类型(Primitives/Value Types) 引用类型(Reference Types)
字符串(String) 对象(Object)
数字型(Number) 数组(Array)
布尔类型(Boolean) 函数(Function)
未定义(undefined,未初始化时默认类型)
null(常用来把数据置空)
// 字符串
let name = 'Moss';
// 数字
let age = 30;
// 布尔
let isArray = true;
// 未定义
let whoAmI = undefined;
let whoAmI;
// null
let colorSet = null;

// 对象
let person = {
  name: 'Moss',
  age: 30
};
// 访问对象内的属性
person.name = '550W';
person['age'] = 20;   // 用双引号来包裹属性也可以
// 动态访问属性
let selection = 'name';
person[selection] = 'Mary';

// 数组,动态扩展,类型可变
let selectedColors = ['red', 'blue'];
selectedColors[0] = 'not red';
selectedColors[2] = 1;

// 函数
function greet(name){
  console.log('Hello ' + name);
}
greet('John');
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇