JavaScript

JavaScript

 JavaScript
一定加分号
引用方式
  <script src='xxx.js'></script>

注释
  // 单行
  /* */ 多行

基本数据类型

全局变量
  name = 'a'
局部变量
  var name = 'a'

字符串
  a = "hnimix"
  a.charAt(n) #获取字符串下标n的字符
  a.substring(1,3) >=1, <3
  a.length #get length of the string
  a.trim() #remove space
数字
  parseInt() #字符串到整型
浮点型

定时器
  setInterval(execute_code, time/ms)

  clearInterval(name)

boolean
  true/false

list:
  obj.join(split)
    [11,22,33,44].join('--') result is 11--22--33--44

dict:
  a= {'k1':'v1', 'k2':'v2'}

for:
  for(var item in a){
    pass;
  }

  for(var i=0;i<10;i=i+1){
    console.log(item);
  }

if
  if(condition){
    pass
  }else if(condition){
    pass
  }else {
    pass
  }

1 == '1' #值相等
!=
1 === '1' #值必须相等且类型一致
!==
and: &&
or: ||

switch
  switch(name){
    case: '1':
    console.log(1);
    case: '2':
    console.log(2);
    case: '3':
    console.log(3);
  }

function:
  普通函数
    function function_names
  匿名函数
    setInterval(function(){pass}, 5000)
  自执行函数
    (function(arg){
      console.log(arg);
    })(1)

序列化
  JSON.stringify(li) #将对象转换为字符串
  JSON.parse() #将字符串转换为对象

转义
  decodeURI( ) # URl中未转义的字符
  decodeURIComponent( ) # URI组件中的未转义字符
  encodeURI( ) # 转义字符
  encodeURIComponent( ) # 转义URI组件中的字符,能转义斜杠
  escape( ) # 对字符串转义
  unescape( ) # 给转义字符串解码
  URIError # 由URl的编码和解码方法抛出

eval
  same as python

时间
  date对象
  var d = new Date()

作用域
  以函数作为作用域 let
  所有的作用域在创建函数且未执行时候就已经存在
  作用域链,调用之前存在
  js局部变量会提前声明

js面向对象:
function foo(){
  this.name = 'hinimix'
  this.sayName = function(){
  console.log(this.name);
  }
}

var obj = new foo();

- this 代指对象
- 创建对象时,用函数new()

js的原型
function foo(){
  this.name = 'hinimix'
  this.sayName = function(){
  console.log(this.name);
  }
}

foo.prorotype = {
  'sayName':function(){
  console.log(this.name)
  }
}

正则
test //判断字符串是否符合规定的正则,默认整体全匹配
rep = /^d+$/;
rep.test("2222222")
true
exec //获取匹配的数据,默认只匹配第一个

g //全局匹配
i //不区分大小写
m //多行,默认多行

示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .hide {
            display:none;
        }
    </style>
</head>
<body>
    <div style="z-index: 1">
        <div id="d1", class="c1 c3", class="c2">来最后跳一支悲伤优雅的舞蹈就散场...</div>
        <div id="d2">test div</div>
        <input type="button" value="register"  onclick="register()">
    </div>

    <div class="c1 hide"; id="reg1"; style="position: fixed; height:100px; 300px; top:50%; left: 50%;
    margin-left:-150px; margin-top:-50px; background-color: coral; z-index: 100">
        <input type="text" value="username" style="display: block;">
        <input type="password" style="display: block;">
        <input type="submit"  value="submit">
        <input type="reset">
    </div>

    <div class="c2 hide"; id='zzc'; style="position: fixed; top: 0px; bottom: 0px; left: 0px; right: 0px;
    background-color: gray;opacity: 0.5; z-index: 10">

    </div>

</body>
</html>

<script>
    function test(){
        p = document.getElementById('d1')
        i = p.innerText
        j = i.charAt(0)
        k = i.substring(1, i.length)
//        alert(j,k)
        p.innerText = k + j
    }

    setInterval('test()', 500)

    function register(){
        document.getElementById('zzc').classList.remove('hide')
        document.getElementById('reg1').classList.remove('hide')
    }
</script>
原文地址:https://www.cnblogs.com/hinimix/p/9270062.html