Javascript 基础知识之学习笔记

系列文章目录

提示:阅读本章之前,请先阅读目录



前言


Null

// 1. Null类型的值只有一个,它就是null
const student = null

// 2. null 就是一个空的对象
console.log(typeof student) // object

Undefined

// 1. 没有给变量赋值,那么,值就是undefined
var b; // undefined

// 2. 类型,也是undefined
console.log(typeof b) // undefined

toString

// 1. 不会影响原变量,会返回新的字符串
const name = 123
const nameStr = name.toString()

// 2. 无法对null和undefined转换
const testNull = null;
const testUndefined;
console.log("我会报错,因为我根本就没有toString的方法", testNull.toString())
console.log("我会报错,因为我根本就没有toString的方法", testUndefined.toString())

// 3. 可以使用String()函数转换null和undefined
console.log("用我可以", String(testNull))
console.log("用我可以", String(testUndefined))

Number

// 1. 如果包含字符串,则返回NaN
const num1 = Number("123px")

// 2. 如果是空字符串,则返回0
const num2 = Number("    ")

// 3. 如果是null,则返回0
const num2 = Number(null)

// 4. 如果是undefined,则返回NaN
const num2 = Number(undefined)

// 5. 如果是true,则返回1
const num2 = Number(true)

// 6. 如果是false,则返回0
const num2 = Number(false)

立即执行函数

// 1. 匿名函数
function() {
	console.log("我被调用了")
}

// 2. 立即执行
(function() {
	console.log("我被调用了")
})();

// 3. 传参
(function(a,b,c) {
	console.log("我被调用了")
})(1,2,3);

工厂函数

function createObject(name, age) {
	const obj = new Object();
	obj.name = name
	obj.age = age
	return obj;
}

const a = createObject("a", 18)
const b = createObject("b", 26)

垃圾回收GC

在这里插入图片描述

在这里插入图片描述

call 和 apply

在这里插入图片描述

// 1. 可以更变函数的this指向
function hello(a, b) {
	console.log("this指向", this)
}

hello() // this指向是window
const obj1 = {}
hello.call(obj1) // this指向是obj1
hello.apply(obj1) // this指向是obj1

// 2. call 第二个参数,第三个参数 。。。 作为实参
hello.call(obj1,1,2)

// 3. apply 第二个参数,必须是个数组
hello.apply(obj1, [1,2])

arguments

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值