如下引入外部js文件script.js
,在script.js
中打印了对应id是root的元素。但是因为我们在head标签中先引入的外部文件,然后才去渲染的dom,所以此时结果为 null。
<html>
<head>
<script src="script.js"></script>
</head>
<body>
<div id="root">sdsdassa</div>
</body>
</html>
script.js
console.log(document.getElementById('root'))
此时在,script标签中添加async
或者defer
,这就是延迟加载
<script src="script.js" async></script>
<script>
<script async>
<script defer>
string number boolean undefined null symbol bigint
object(array function)
valueOf()
或者toString()
。具体使用哪个方法与调度机制有关。// 对两个方法重写
const foo ={
toString(){
return 'toString'
}
valueOf(){
return 1
}
}
alert(foo) // 隐式转换为'toString' 使用了toString
console.log(1+foo) // 2 使用了 valueOf
null 和 undefined的区别
先有null 后有 undefined,null会被隐式的转换为0,并且是一个对象,很难发现错误。
undefined是为了填补之前的坑。
具体区别:
null是一个表示‘无’的对象(空对象指针),转为数值是0.
undefined是一个表示“无”的原始值/基本数据类型,转为数值是NaN