js note

1.在这里插入图片描述
在这里插入图片描述
2.正则表达式:
/(\w)\1(\d)\2/
()又叫做子表达式
\1反向引用第一个子表达式匹配到的内容,\2以此类推
eg:str.replace(reg,"$1$2");

str.replace(reg,function($,$1,$2){
return $1 + $2;返回字符
})
$:每次正则表达式匹配(查找)的结果
$1;每次正则表达式第一个子表达式匹配到的结果,$2以此类推
reg每匹配(查找)一次,fun执行一次
正则表达式默认都是贪婪匹配模式, 在修饰匹配次数的特殊符号后再加上一个 “?” 号,如:\w+?,则可以使匹配次数不定的表达式尽可能少的匹配,使可匹配可不匹配的表达式,尽可能的 “不匹配”。这种匹配原则叫作 “非贪婪” 模式,也叫作 “勉强” 模式。
3. ‘abc’ + localStorage.id || 1,如果localStorage.id未定义(undefined),则结果为’abcundefined’,不会执行到 || 1
4. split(’,’):str分割为array,
array.slice(start.end):选取部分
str.slice(start,end-1)
substring(from,to(非负,不含))
解构:…[1,2]数组转为“,”分割序数
eg:console.log(5,…[1,2,3]) => 5,1,2,3
[…”hello”]=>[’h’,’e’,’l’,’l’,’o’]
[a,…rest]=[1,2,3,4] =>a=1,rest = [2,3,4]
[a,…rest]=[1] =>a=1.rest=[]
[a,…rest]=[] =>a=undefined,rest=[],
[…first,last] = [1,2,3] 报错,
若…用于数组赋值,只能放参数尾部,
[…arr1,…arrr2] == concact:不改变原 ,返回新数组
…能够识别Unicode字符,
match,成功返回匹配的数据,失败返回null
arr.includes(se,st) 是否含有se
arr:map(fn) 同步,forEach(fn) 异步
arr.splice(index,many,item)
push()加尾,unshift()加头,返length
pop():删尾,shift()删头,返删item

// typeof 判断基础类型
typeof function === 'function'
typeof NaN === 'number'
typeof null === 'object'

Object.prototype.toString.apply([]) === '[object Array]'
Object.prototype.toString.apply({}) === '[object Object]'
Object.prototype.toString.apply(function(){}) === '[object Function]'
Object.prototype.toString.apply(null) === '[object Null]'
Object.prototype.toString.apply(undefined) === '[object Undefined]'
// IE6/7/8 Object.prototype.toString.apply(null)返回 [object Object]

在这里插入图片描述

let person = {
  // get,set内不能访问自己,切其属性的writable不能设为true
  $age: 10,
  get age() {
    return this.$age
  },
  set age(val) {
    this.$age = val
  },
  sex: {
    toJSON() {
      return 'man'
    }
  }
}
Object.defineProperty(person, "name", {
  // 默认配置项都是false
  configurable: true,
  writable: true,
  enumerable: true,
  value: 'xinxin'
})
Object.defineProperty(person, "age", {
  enumerable: true,
  get() {
    return 10
  }
})
Object.defineProperties(person, {
  name: {
    configurable: true,
    writable: true,
    enumerable: true,
    value: 'xinxin'
  },
  luck: {
    configurable: true,
    get() {
      return this.name
    }
  },
  salay: {
    set(val) {
      this.name = val
    }
  }
})
Object.getOwnPropertyDescriptor({ name: 'xinxin' }, "name")

数字1.0000或48.574000自动去掉小数点后多余的0,转为有效数值

console.log("%c css占位符!", "color: red; font-size: 16px");
console.log(' %s + %s = %s',1,2,3);
%s  字符串
%d	整数
%i	整数
%f	浮点数
%o	对象的链接
%c	css格式字符串

// 错误
export default var a = 1
import * as circle from ‘./circle’;

import命令输入的变量都是只读的,因为它的本质是输入接口。也就是说,不允许在加载模块的脚本里面,改写接口

import {a} from './xxx.js'

a = {}; // Syntax Error : 'a' is read-only;

上面代码中,脚本加载了变量a,对其重新赋值就会报错,因为a是一个只读的接口。但是,如果a是一个对象,改写a的属性是允许的。

import {a} from './xxx.js'

a.foo = 'hello'; // 合法操作

上面代码中,a的属性可以成功改写,并且其他模块也可以读到改写后的值。不过,这种写法很难查错,建议凡是输入的变量,都当作完全只读,不要轻易改变它的属性

注意,模块整体加载所在的那个对象(上例是circle),应该是可以静态分析的,所以不允许运行时改变。下面的写法都是不允许的

import * as circle from './circle';
// 下面两行都是不允许的
circle.foo = 'hello';
circle.area = function () {};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值