Reference

网页的动作语言

Examples are better than 1000 words.——一例胜千言

引用

<head></head>

<body></body>

<script></script>

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

输出

  • innerHTML

document.getElementById("demo").innerHTML='Hello World!';
  • document.write()

  • window.alert()

  • console.log()

  • print()

控制

  • getElementById("demo")

  • getElementsByClassName("demo")

  • getElementsByTagName("p")

  • querySelectorAll("p.intro")

声明

  • var

  • let

  • cost

注释

  • //单行注释

  • /*多行注释*/

运算符

  • +

  • -

  • *

  • **

  • /

  • %

  • ++

  • --

  • &&

  • ||

  • !

  • ==

  • ===

  • !=

  • !==

  • >

  • <

  • >=

  • <=

  • ?

  • +=

  • -=

  • *=

  • /=

  • %=

  • **=

函数

function myFunction(p1, p2) {
  return p1 * p2;   // The function returns the product of p1 and p2
}
  • 数组:x = ["a","b","c"]

  • 物体:x={name:"fiat",model:"modern",color:"white"}

字符串处理

  • .length

  • indexOf()

  • lastIndexOf()

  • search()

  • slice()

  • substring()

  • substr()

  • replace()

  • toUpperCase()

  • toLowerCase()

  • concat()

  • trim()

  • charAt()

  • charCodeAt()

  • split()

数字

Js语法中,支持数位是15位,以及小数点后17位。

NaN = not a number

isNaN()

数字进制

var myNumber = 32;
myNumber.toString(10);  // returns 32
myNumber.toString(32);  // returns 10
myNumber.toString(16);  // returns 20
myNumber.toString(8);   // returns 40
myNumber.toString(2);   // returns 100000

数字变成物体

var x = new Number(124);
  • toExponential()

  • toFixed()

  • toPrecision()

数组

var cars = ["Saab", "Volvo", "BMW"];||var cars = new Array("Saab", "Volvo", "BMW");
  • array.foreach()

  • array.push()

  • array.pop()

  • typeof.array

  • array.join("*")

  • array.shift()

  • array.unshift()

  • delete array[0]

  • array.splice(2,0,"apple","orange")

  • array.concat(array)

  • sort()

  • reverse()

调用

person.lastName;||person["lastName"];

时间

  • Date(year, month, day, hours, minutes, seconds, milliseconds)

  • toUTCString()

  • toISOString()

  • toDateString()

数学

  • Math.PI

  • Math.round()

  • Math.pow()

  • Math.sqrt()

  • Math.abs()

  • Math.ceil()

  • Math.floor()

  • Math.sin()

  • Math.cos()

  • Math.min()

  • Math.max()

  • Math.random()

  • Math.E

  • Math.SQRT2

  • Math.SQRT1_2

  • Math.LN2

  • Math.LN10

  • Math.LOG2E

  • Math.LOG10E

逻辑

逻辑

True or False

if (condition) {
  //  block of code to be executed if the condition is true
} else {
  //  block of code to be executed if the condition is false
}
  • Boolean()

  • variablename = (condition) ? value1:value2

if (condition1) {
  //  block of code to be executed if condition1 is true
} else if (condition2) {
  //  block of code to be executed if the condition1 is false and condition2 is true
} else {
  //  block of code to be executed if the condition1 is false and condition2 is false
}
switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}
switch (new Date().getDay()) {
  case 6:
    text = "Today is Saturday";
    break;
  case 0:
    text = "Today is Sunday";
    break;
  default:
    text = "Looking forward to the Weekend";
}

Last updated