欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ES6/7学习之基础复习1

程序员文章站 2023-12-21 22:18:34
...

数据类型

简单数据/基本数据/原始类型:String Number Boolean null undefined Symbol
复杂数据:Object
对象类型/引用类型/Object:Object Array Function Date RegExp

对象方法

String对象
toString valueOf toLowerCase toUpperCase toLocaleLowerCase toLocaleUpperCase
indexOf lastIndexOf includes slice split concat
repeat match search replace startsWith endsWith trim
substr substring charAt charCodeAt fromCharCode

Array对象
toString valueOf indexOf lastIndexOf includes slice splice push unshift shift pop join concat fill
sort reverse filter forEach map some keys values entries find findIndex
reduce reduceRight copyWith every
Array.isArray Array.from

Date对象[new Date()]
Date.now()
getTime getFullYear getMonth getDate getDay getHours getMinutes getSeconds getMilliseconds
setTime setFullYear setMonth setDate setHours setMinutes setSeconds setMilliseconds
valueOf() toString() toDateString() toTimeString
toLocaleString toLocaleDateString toLocaleTimeString

Math对象
ceil floor round random max min

全局方法
String Number parseInt parseFloat isNaN
encodeURI decodeURI encodeURIComponent decodeURIComponent escape unescape

变量提升

声明方式
字面量声明 var a = …;
函数声明 function a() {}

声明:var a;
初始化:a = 1;

  1. 所有的声明都会提升到作用域顶部。
  2. 同一个变量只会声明一次,其它的会被忽略或覆盖。
  3. 函数声明优先级高于字面量声明,并且函数声明和函数定义的部分一起提升。
  4. let const声明不存在提升
console.log(typeof a); // function
console.log(typeof b); // function
var a = 1;
function a() {};
function b() {};
var b = 1;
console.log(a); // undefined
console.log(b); // f b() {}
var a = function() {};
function b() {}
b();
var a = 1;
function b() {
	console.log(a); // 1
}
b();
var a = 1;
function b() {
	// var a; // a的声明被提升到函数作用域顶部
	console.log(a); // undefined
	var a = 1;
}
// let const 声明不存在变量提升
console.log(a); // 报错:a is not defined
let a = 1;
console.log(b); // 报错:b is not defined
const b = 2;

严格模式"use strick"

严格模式指定一个script标签内的代码在严格条件下执行。
“use strict” 指令只允许出现在脚本或函数的开头

<script>
"use strict"
a=1; // 严格模式下不允许使用未声明变量,此处报错:a is not defined
</script>
<script>
b=1; // 此script标签内未指定严格模式,b=1生效
</script>
a=1;
"use strict" // 此行未在脚本开头,指定严格模式失败
b=2;
console.log(a) // 1

严格条件:

  1. 严格模式下不允许使用未声明的变量
  2. 函数参数不能重名
  3. 函数内this默认不再指向window,默认为undefined。
  4. delete删除不可配置的属性报错
"use strict"
a = 1; // 报错:a is not defined
b = 2; // 不会报错
var b;
"use strict"
function add(num,num) {} // 报错:Duplicate parameter name not allowed in this context(此上下文中不允许有重复的参数名)
function add(num,num) {
	console.log(num); // num赋值两次,后面的覆盖前面的。
}
add(1,2); // 2
"use strict"
function show () {
	var obj = {
		name:'乔峰',
		say: function() {
			console.log(this);
		}
	};
	console.log(this); // 调用时this指向undefined
	obj.say();
}
show(); // 在最外部调用show
/*输出:
undefined
{name: "乔峰", say: ƒ}
*/
var newShow = new show();
/*输出:
show {}
{name: "乔峰", say: ƒ}
*/
function show () {
	var obj = {
		name:'乔峰',
		say: function() {
			console.log(this);
		}
	};
	console.log(this); // 调用时this指向全局对象Window
	obj.say();
}
show(); // 在最外部调用show
/*输出:
Window
obj
*/
// 使用var let const 声明的变量不可配置,不能被delete删除,非严格模式下删除返回false
var a = 1;
console.log(Object.getOwnPropertyDescriptor(window,'a')); // 返回指定对象上一个自有属性对应的属性描述符
/* 输出:
{
	configurable: false // 表示不可配置(不可被删除)
	enumerable: true
	value: 1
	writable: true
}
*/
delete a; // false 不会报错
"use strict"
var a = 1;
delete a; // 报错:Delete of an unqualified identifier in strict mode.(在严格模式下删除非限定标识符。)

let const

使用let const声明的变量不属于全局对象window

var a = 1;
let b = 2;
const c = 3;
console.log(window.a); // 1
console.log(window.b); // undefined
console.log(window.c); // undefined
相关标签: ES6/7 js

上一篇:

下一篇: