Javascript一:基础类型

script元素

有两种方式使用script元素,直接在页面嵌入或者引入外部文件。默认自上而下执行。
在解释器对script元素内部的代码求值完毕前,页面的其余内容不会被浏览器加载或显示。
解析引入的外部文件时,页面的处理也会暂停。浏览器遇到body标签才会呈现内容,所以一般把js代码放在body中页面内容的后面。这样可以缩短页面内容呈现的时间。

js文件外部引用,可以增加可维护性,可缓存,浏览器能根据设置缓存外部js文件,如果有多个页面使用,可以加快页面加载速度。

script和img元素,它们的src属性可以指向外域的URL。利用这一点可以实现跨域。

script中的其它属性

无属性:不再渲染页面,同步下载并执行指定的脚本。
defer: 立即异步下载,但整个页面解析完(遇到html结束标签),再按照页面出现的顺序依次运行脚本。
async: 立即异步下载,异步加载页面的其它内容。下载完,渲染引擎就会中断渲染,执行脚本,多个脚本无法保证执行顺序。

定义变量

1.可以使用一条语句定义多个变量

let message = "hi", found = false, age = 29;

2.使用连等定义变量后面的变量会成为全局变量,应避免

function test() {
  let p1 = p2 = 3;
}
test();
console.log(p2)//3
console.log(p1)//p1 is not defined

3.var、let 、 const的区别

var 声明的变量,function定义函数会被提升。当出现重名时,都提升后,function定义函数的覆盖var 声明的变量。

console.log(a) // ƒ a() {}
function a() {}
var a = 1

let和 const申明的变量

1.不会提升,且在全局作用域下使用 letconst 声明变量,不会被挂载到 window 上。

2.当我们在声明 a 之前如果使用了 a,就会出现报错的情况,会暂时性死区, 所以不能在声明前就使用变量

4.变量提升的作用

解决函数间互相调用的情况,假如不存在提升这个情况,不可能存在 test1test2 前面然后 test2 又在 test1 前面。

function test1() {
    test2()
}
function test2() {
    test1()
}
test1()

数据类型

6 种原始类型:Number,String,Boolean,Null,Undefined,symbol

判断为false六种情况:0、NaN、''(空串)、null和undefined,false。

原始类型存储的都是值,是没有函数可以调用的,比如 没有undefined.toString()

(1).toString() 是可以使用。是因为强制转换成了 String 类型,所以可以调用 toString 函数。

原始类型存储的是值,对象类型存储的是地址。

类型判断

typeof

对于原始类型,除了 null 都可以显示正确的类型

对于引用类型,除了函数都会显示 object

typeof 1 // 'number'
typeof '1' // 'string'
typeof undefined // 'undefined'
typeof true // 'boolean'
typeof Symbol() // 'symbol'
typeof [] // 'object'
typeof {} // 'object'
typeof console.log // 'function'

instanceof

可以使用 instanceof,判断一个对象的正确类型。

const Person = function() {}
const p1 = new Person()
p1 instanceof Person // true

const str = 'hello world'
str instanceof String // false

const str1 = new String('hello world')
str1 instanceof String // true

colors =[]
colors instanceof Array //true

instanceof原理

//判断对象的原型链__proto__中是不是能找到类型的 prototype。
function myInstanceof(val, type) {
  let prototype = type.prototype
  val = val.__proto__
  while (true) {
    if (val === null || val === undefined)
      return false
    if (prototype === val)
      return true
    val = val.__proto__
  }
}

Object.prototype.toString

返回 "[object type]"

//一些例举
Object.prototype.toString.call(true)        // [object Boolean]
Object.prototype.toString.call({})          // [object Object]
Object.prototype.toString.call([])         // [object Array]
Object.prototype.toString.call(function(){}) // [object Function]
Object.prototype.toString.call(p1)  //[object Object] 实例对象返回的是Object

其它情况

Infinity 无穷,如果数值超过了上限,会自动转为Infinity,有正负符号。

isFinite()函数可以鉴别数值。返回true或false。

0除以0返回NaN,正数除以0返回Infinity,负数除以0返回-Infinity。

任何涉及NaN的运算都会返回NaN。

isNaN()函数,不能被转换为数值的值会返回true。

四则运算

加法

如果一边是字符串,会将另一方转换为字符串后拼接。

如果不是字符串或者数字,那么会将它转换为数字或者字符串

1 + '1' // '11'
true + true // 2
4 + [1,2,3] // "41,2,3"`
'a' + + 'b' // "aNaN"   因为 `+ 'b'` 等于 `NaN`,所以结果为 `"aNaN"`

其它运算符,被转尝试为数字,无法转换为数字,获得NaN。

1 - '1' // 0
1 - 'a1' // NaN
4 * '2' // 8
4 * [] // 0
[]*[] // 0
4 * [1, 2] // NaN
4 / '2' // 2
4 / [] // Infinity
4 / [1, 2] // NaN

类型转换

转换为字符串

  1. toString()
let num = 1
console.log(typeof num.toString())//string

null和undefined 没有任何方法,使用会报错,其它的使用会返回相应的字符串值表现。

NaN.toString()//"NaN"
  1. String()
//可以将null和undefined返回对应的字符串。
String(undefined)//"undefined"

转换为数值类型

1.Number()

    const s1 = true
    const s2 = '123'
    const s3 = 'abc123'
    const s4 = '123abc'
    console.log(Number(s1))//1
    console.log(Number(s2))//123
    console.log(Number(s3))//NaN
    console.log(Number(s4))//NaN

2.parseInt()

    const s1 = true
    const s2 = '123'
    const s3 = 'abc123'
    const s4 = '123abc'
    console.log(parseInt(s1))//NaN
    console.log(parseInt(s2))//123
    console.log(parseInt(s3))//NaN
    console.log(parseInt(s4))//123

3.parseFloat()

 const s2 = '123.1a'
 console.log(parseFloat(s2))//123.1

4.+和-符号

    const s1 = true
    const s2 = '123.1a'
    const s3 = 'abc123'
    const s4 = '123abc'
    console.log(+s1)//1
    console.log(+s2)//123.1
    console.log(+s3)//NaN
    console.log(+s4)//NaN

逻辑运算符

逻辑与&&和逻辑或||,第一个操作数能决定结果,就不会对第二个求值。

isTrue()&&fn()

== 与 ===

一.== 来说,如果对比双方的类型不一样的话,就会进行类型转换

  1. 首先会判断两者类型是否相同。相同的话就是比大小了

  2. 类型不相同的话,那么就会进行类型转换

  3. 如果是对比 nullundefined,返回 true

  4. 如果两者类型为string 和number ,将字符串转换为number后比较

  5. 如果其中一方是boolean,转换为number再进行判断

  6. 如果其中一方是object 且另一方为 string,number,symbol,把object转为原始类型再进行判断

   [] == ![]  //true
   []==false  //先执行!,![]为false,
   []==0      //boolean转换为number比较(条件5)
   ''==0      //object转为原始类型 [].toString() =''(条件6)

二. === ,判断两者类型和值是否相同。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容