console.log(3 / 2)
console.log(Math.floor(3 / 2))
console.log(0.1 + 0.2)
console.log(Math.sign(3.5))
console.log(2 * Math.PI * 10)
// 输出63
console.log(parseInt('0x3F'));
console.log(parseInt('0x3F', 16));
console.log(parseInt("063"))
console.log(parseInt("63", 10))
console.log(parseInt('77', 8));
console.log(parseInt('111111', 2));
console.log(+ "0x3f")
console.log(NaN + 10)
console.log(isNaN(NaN))
console.log(isNaN(parseInt("2", 2)))
console.log(1 / 0)
console.log(-1 / 0)
console.log(isFinite(Infinity))
console.log(isFinite(NaN))
console.log(isFinite(-Infinity))
console.log(undefined)
/// 字符串
var str = "Hello Word"
console.log(str.length)
console.log(str.split(" "))
console.log(str.charAt(1))
console.log(str.replace("Word", "JsonYe"))
console.log(str.toLowerCase())
console.log(str.toLocaleLowerCase())
console.log(str.toUpperCase())
console.log(str.toLocaleUpperCase())
/// Bool
console.log(Boolean(''))
console.log(Boolean('1'))
console.log(Boolean(null))
console.log(Boolean(NaN))
console.log(Boolean(undefined))
console.log(Boolean(0))
console.log(Boolean(false))
console.log(Boolean(Infinity))
console.log(Boolean(-Infinity))
/// 变量(let,const,var)
// let 声明一个块级作用域的本地变量,并且可选的将其初始化为一个值。
let age = 12
// console.log(index) // 报错
for (let index = 0; index < 2; index++) {
// index 只能在此代码块中被引用
console.log(index)
}
// console.log(index) // 报错
// const 允许声明一个不可变的常量。这个常量在定义域内总是可见的,声明时需要初始化。
const PI = 3.1415926
console.log(PI)
console.log(myVarVariable) // 可以使用
for (var myVarVariable = 0; myVarVariable < 2; myVarVariable++) {
console.log(myVarVariable) // 可以使用
}
console.log(myVarVariable) // 可以使用
/// 运算符
var x = 3
console.log(x)
x += 4
console.log(x)
console.log("hello" + " " + "word")
console.log("3" + 4 + 5)
console.log(3 + "4" + 5)
console.log(3 + 4 + "5")
console.log(123 == "123")
console.log(1 == true)
console.log(0 == false)
/// 控制语句
var name = "JsonYe"
if (name == "JsonYe") {
name += " !"
} else {
name = "! " + name
}
console.log(name)
var flag = 0
var sum = 0
while (flag <= 10) {
sum += flag
flag++
}
console.log(sum)
sum = 0
flag = 0
do {
sum += flag
flag++
} while (flag <= 10)
console.log(sum)
sum = 0
flag = 0
for (; flag <= 10; flag++) {
sum += flag
}
console.log(sum)
var uName = "Json"
uName = NaN
var nName = uName || "json"
console.log(nName)
var obj = new Object()
console.log(obj)
var obj2 = {
name: "JsonYe",
age: 18,
book: {
name: "Android",
price: 12
}
}
console.log(obj2["name"])
console.log(obj2.name)
console.log(obj2["book"]["name"])
console.log(obj2.book.name)
function Person(name, age, book) {
this.name = name;
this.age = age;
this.book = book;
}
function Book(name, price) {
this.name = name;
this.price = price;
}
var jsonYe = new Person("JsonYe", 18, new Book("Android", 12));
console.log(jsonYe.name)
console.log(jsonYe["name"])
console.log(jsonYe.book.name)
console.log(jsonYe["book"]["name"])
console.log(jsonYe.book.price)
console.log(jsonYe["book"]["price"])
/// 数组
var array = new Array()
array.push("A")
array[2] = "B"
array[100] = 100
array.push("C")
array.push(jsonYe)
console.log(array.length)
console.log(array.join(","))
console.log(typeof (array[99]))
console.log(typeof (array[100]))
console.log(typeof (array[101]))
console.log(typeof (array[102]))
function add() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
sum += arguments[i];
}
return sum;
}
// function avg() {
// var sum = 0;
// for (let index = 0; index < arguments.length; index++) {
// sum += arguments[index]
// }
// return sum / arguments.length;
// }
function avg(...args) {
var sum = 0;
for (let index = 0; index < args.length; index++) {
sum += args[index]
}
return sum / args.length;
}
console.log(add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
console.log(avg(1, 2, 4))
console.log(avg.apply(null, [1, 2, 3]))
/// 匿名
var avg2 = function () {
var sum = 0;
for (let index = 0; index < arguments.length; index++) {
sum += arguments[index];
}
return sum / arguments.length;
}
console.log(avg2(1, 2, 3, 4, 5, 6))
function t() {
var a = 1;
var b = 2;
// 定义了一个匿名行数
(function () {
var b = 3;
a += b;
}());// 执行匿名函数
console.log("a=" + a + ",b=" + b)
}
t()
function fb(n) {
if (n == 1) return 0;
if (n == 2) return 1;
return fb(n - 1) + fb(n - 2);
}
console.log(fb(40))
/// 闭包
function makeAdd(a) {
return function (b) {
return a + b;
}
}
var add5 = makeAdd(5)
var add20 = makeAdd(20)
console.log(add5(6))
console.log(add20(6))
class Polygon {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
set name(value) {
this._name = value;
}
talk() {
console.log(this._name);
}
static PI = 3.1415
static sex() {
console.log("男")
}
}
const poly1 = new Polygon("JsonYe");
poly1.talk()
poly1.name = "LiuLi";
poly1.talk()
// console.log(poly1.PI)
// poly1.sex()
console.log(Polygon.PI)
Polygon.sex()
function PolygonTwo(name) {
this.name = name;
}
PolygonTwo.prototype.speak = function () {
return this.name;
}
PolygonTwo.prototype.add = function (a, b) {
return a + b;
}
console.log(new PolygonTwo("JsonYe").speak())
console.log(new PolygonTwo("").add(2, 5))
class Rectangle {
#num;
constructor(height, width) {
this.height = height;
this.width = width;
this.#num = 3;
}
speak() {
return `${this.#num}-${this.height}`
}
}
var rg = new Rectangle(12, 20);
console.log("rg.height:" + rg.height)
rg._height = 15;
console.log("rg._height:" + rg._height);
Rectangle.staticWidth = 20;
console.log("Rectangle.staticWidth:" + Rectangle.staticWidth)
console.log(rg.speak())
/// 箭头函数
const items = [
"Goods", "Pad", "Apple", "Pick"
]
console.log(items.map(item => item.length).join(","))