本周学习总结(原生)
访问不存在的方法,会报错
class Dog{
constructor(name) {
this.name=name;
}
}
Dog.add(); //会报错
//TypeError: Dog.add is not a function
引入的模块是只读的,不能被修改
// counter.js
let counter=10;
export default counter;
//index.js
import myCounter from './counter.js'
myCounter+=1;
console.log(myCounter)//所有报错
delete操作符
delete操作符返回一个布尔值:true 删除成功,否则返回false
但是通过 var, const 或 let 关键字声明的变量无法用 delete 操作符来删除。
主要删除对象的属性
defineProperty
可以给对象添加一个新属性,候着修改已经存在的属性,而我们使用
defineProperty
方法添加了一个属性之后,属性默认为不可枚举(notenumerable),所有无法通过Object.keys()
显示
import
import 命令是编译阶段执行,就是导入的模块先运行,而导入模块的文件后执行
区别
使用
require()
运行时执行
模块化
传统的命名空间
(function(w){
w.b=2
})(window)
ES6模块化
// math.js
export let val = 1
export function add () {
val++
}
//另一种
module.exports=add;
// test.js
import { val, add } from './math.js'
console.log(val) // 1
add()
console.log(val) // 2
面试
https://www.cxymsg.com/guide/preface.html#%E5%A4%A7%E9%87%8F%E7%9A%84%E8%BF%87%E6%97%B6%E9%97%AE%E9%A2%98
排序
console.log('cbga'.split``.sort())
异步问题
function takeLongTime() {
return new Promise(res => {
setTimeout(function () {
res();
console.log(1)
}, 1000)
})
}
async function test() {
await takeLongTime();
console.log(2)
}
strman 字符串操作
const appendArray = (value, appends) => {
if (appends.length == 0) {
return value
}
return value + appends.join('')
}
const append = (value, ...append) => appendArray(value, append)
console.log(appendArray('a', ['x', 'g', 'd'])) //axgd
console.log(append('a', 'x', 'c', 'b')) //axcb
const substr = (value, start, length) => value.substr(start, length);
const at = (value, index) => substr(value, index, 1);
console.log(substr('abcder', 0, 1)) //a
console.log(at('abcder', 2)) // c
const base64Encode = value => Buffer.from(value)
.toString('base64');
const base64Decode = value => Buffer.from(value, 'base64')
.toString();
console.log(base64Encode('strman')) // 'c3RybWFu'
console.log(base64Decode('c3RybWFu')) // strman
// 编码
// 每一个字符的长度是16,=>charCodeAt(0)=>转成二进制toString(2)=>padStart多了的填充 0
//合并join
const encode = (value, length, base) => value.split('')
.map(val => val.charCodeAt(0)
.toString(base)
.padStart(length, '0'))
.join('');
console.log(encode('abc', 16, 2))
//反码
const decode=(value,length,base)=>{
let i=0,arr=[]
while (i < value.length / length) {
arr.push(value.slice(i*16,16+i*16))
i++
}
return arr.map(val=>String.fromCharCode(parseInt(val,base))).join('')
}
console.log(decode('000000000110000100000000011000100000000001100011', 16, 2))
//abc
### encodeURL
let test1="http://www.w3school.com.cn/My我是谁哈哈哈"
console.log(encodeURI(test1))
//http://www.w3school.com.cn/My%E6%88%91%E6%98%AF%E8%B0%81%E5%93%88%E5%93%88%E5%93%88
### decodeURI
let test2 = 'http://www.w3school.com.cn/My%E6%88%91%E6%98%AF%E8%B0%81%E5%93%88%E5%93%88%E5%93%88';
console.log(decodeURI(test2))
const truncate = (str, length, _append) =>str.substr(0,length-_append.length)+_append;
const title = 'A Javascript string manipulation library.'
console.log(truncate(title, 16, '...'))
//A Javascript ...
改变嵌套条件
const printBlack = () => console.log(1);
const printRed = () => console.log(2);
const printBlue = () => console.log(3);
const colorObj = {
black: printBlack,
red: printRed,
blue: printBlue,
};
let item='red';
if (item in colorObj) {
colorObj[item]() //2
}
q.js
//Array.prototype.slice
const array_indexOf = (value) => {
return function (item) {
for (let i = 0; i < value.length; i++) {
if (item == value[i]) {
return i
}
}
return -1
}
}
console.log(array_indexOf(['a', 'b', 'c', 'd', 'f', 't'])('t'))
// Object.keys Object.hasOwnProperty
const object_keys = object => {
const keys = []
for (let key in object) {
keys.push(key)
}
return keys
}
console.log(object_keys({name: 'xxx', age: 12}))
// [ 'name', 'age' ]
let a = Promise.all([Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)])
a.then(res => {
console.log(res)
})
isFinite()
如果number是NaN,或者正负无穷大的数,则返回false
..........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
决定自己的高度的是你的态度,而不是你的才能
记得我们是终身初学者和学习者
总有一天我也能成为大佬