1)a??b
如果 a 为 undefined 或 null 则返回b ,否则直接返回a;
2)a?.b
如果 a 为 undefined 或 null 则返回 undefined ,否则直接返回a.b;
var obj ={} console.log(obj?.a?.b ?? 233 ) //233
var obj={a:{b:1}} console.log(obj?.a?.b??233) //1
------------------------------------------------------------------------------
<html>
<script>
let a1 = false;
var b1;
console.log(a1 = (a1??b1??true)); //50;
console.log(a1); //NULL
let a=null;
let c=30;
console.log(a??c??d??100);
let obj= {
person:{
firstName:"John",
lastName:"Doe"
},
occupation: {
compony:'capscode',
position:'developer'
},
human:false,
fullName: function(){
console.log("Full Name is: "+ this.person.firstName+" >"+this.person.lastName)
}
}
console.log(obj . person . firstName) //John
//console.log(obj . human . award) //TypeError: Cannot read property 'award' of undefined
//console.log(obj ?. human . award) //TypeError: Cannot read property 'award' of undefined
console.log(obj . human ?. award) //undefined
delete obj?.firstName; // delete obj.firstName if obj exists
obj . fullName ?.() //logs John Doe
obj ?. fullName() //logs John Doe
//obj . fullDetails() // TypeError: obj.fullDetails is not a function
//obj ?. fullDetails() // TypeError: obj.fullDetails is not a function
obj.fullDetails ?. () //undefined
function stringToArrayBuffer(str) { // utf16 不管是字符还是汉字
let buffer = new ArrayBuffer(str.length * 2);
let view = new Uint16Array(buffer)
for (let i = 0; i < str.length; i++) {
view[i] = str.charCodeAt(i)
}
return buffer
}
function ArrayBufferToString(buf) {
return String.fromCharCode(...new Uint16Array(buf))
}
function request(url, method = "get") {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open(method, url, true);
// xhr.responseType = 'arraybuffer';
xhr.onload = function () {
resolve(xhr.response);
}
xhr.send();
})
}
request('https://www.baidu.com/').then(arraybuffer => {
console.log(arraybuffer);
return;
// arraybuffer = stringToArrayBuffer("women我们cc1234我们我们我们我们我们7777abc");
let b = new Blob([arraybuffer]);
let blobUrl = URL.createObjectURL(b);
let a = document.createElement('a');
a.href = blobUrl;
a.download = 'a.txt';
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(blobUrl)
})
//console.log(ArrayBufferToString(stringToArrayBuffer("1122")));
</script>
</html>