赞
踩
目录
面向对象和面向过程对比
创建类和对象
类中添加方法
- eg:
- class car {
- constructor(uname,age) {
- this.uname=uname;
- this.age=age;
- }
- sing(s) {
- console.log(s);
-
- }
- }
- var fa=new car("li",12);
- console.log(fa.uname);
- fa.sing('a');
继承(extends)
- class father {
- constructor(x,y) {
- this.x=x;
- this.y=y;
- }
- sum(x,y) {
- console.log(this.x+this.y);
- }
- }
- class Son extends father {
- constructor(x,y) {
- super(x,y);//调用父类的构造函数
- }
- }
- var son=new Son(1,2);
- var son1=new Son(11,22);
- son.sum();
- son1.sum();

super关键字
注:
子类继承父级同时扩展自己的方法
- class father {
- constructor(x,y) {
- this.x=x;
- this.y=y;
- }
- sub() {
- console.log(this.x+this.y);
- }
- }
- // var s=new father(3,2);
- // s.sub();
- class son extends father {
- constructor(x,y) {
- // 利用super调用父类函数
- // super必须在子类this之前调用
- super(x,y);
- this.x=x;
- this.y=y;
- }
- subs() {
- console.log(this.x-this.y);
- }
- }
- var sona=new son(4,3);
- sona.sub();
- sona.subs();

注意:1、ES6中没有变量提升,必须先定义类才能实例化对象
2、类中里面的共有的属性和方法一定要加this使用
3、this指向问题(constructor指向的是实例化对象,方法里面的指方法调用者)
eg:
- // 1. 利用 new Object() 创建对象
-
- var obj1 = new Object();
-
- // 2. 利用 对象字面量创建对象
-
- var obj2 = {};
-
- // 3. 利用构造函数创建对象
- function Star(uname, age) {
- this.uname = uname;
- this.age = age;
- this.sing = function() {
- console.log('我会唱歌');
-
- }
- }
-
- var ldh = new Star('刘德华', 18);
- var zxy = new Star('张学友', 19);
- console.log(ldh);
- ldh.sing();
- zxy.sing();

静态成员和实例成员
eg:
- // 构造函数中的属性和方法我们称为成员, 成员可以添加
- function Star(uname, age) {
- this.uname = uname;
- this.age = age;
- this.sing = function() {
- console.log('我会唱歌');
-
- }
- }
- var ldh = new Star('刘德华', 18);
- // 1.实例成员就是构造函数内部通过this添加的成员 uname age sing 就是实例成员
- // 实例成员只能通过实例化的对象来访问
- console.log(ldh.uname);
- ldh.sing();
- // console.log(Star.uname); // 不可以通过构造函数来访问实例成员
- // 2. 静态成员 在构造函数本身上添加的成员 sex 就是静态成员
- Star.sex = '男';
- // 静态成员只能通过构造函数来访问
- console.log(Star.sex);

eg:
- <script>
- // 1. 构造函数的问题.
- function Star(uname, age) {
- this.uname = uname;
- this.age = age;
- // this.sing = function() {
- // console.log('我会唱歌');
-
- // }
- }
- Star.prototype.sing = function() {
- console.log('我会唱歌');
- }
- var ldh = new Star('刘德华', 18);
- var zxy = new Star('张学友', 19);
- console.log(ldh.sing === zxy.sing);
- // console.dir(Star);
- ldh.sing();
- zxy.sing();
- // 2. 一般情况下,我们的公共属性定义到构造函数里面, 公共的方法我们放到原型对象身上
- </script>

eg:
- <script>
- function Star(uname, age) {
- this.uname = uname;
- this.age = age;
- }
- Star.prototype.sing = function() {
- console.log('我会唱歌');
- }
- var ldh = new Star('刘德华', 18);
- var zxy = new Star('张学友', 19);
- ldh.sing();
- console.log(ldh); // 对象身上系统自己添加一个 __proto__ 指向我们构造函数的原型对象 prototype
- console.log(ldh.__proto__ === Star.prototype);
- // 方法的查找规则: 首先先看ldh 对象身上是否有 sing 方法,如果有就执行这个对象上的sing
- // 如果么有sing 这个方法,因为有__proto__ 的存在,就去构造函数原型对象prototype身上去查找sing这个方法
- </script>

eg:
- <script>
- function Star(uname, age) {
- this.uname = uname;
- this.age = age;
- }
- // 很多情况下,我们需要手动的利用constructor 这个属性指回 原来的构造函数
- // Star.prototype.sing = function() {
- // console.log('我会唱歌');
- // };
- // Star.prototype.movie = function() {
- // console.log('我会演电影');
- // }
- Star.prototype = {
- // 如果我们修改了原来的原型对象,给原型对象赋值的是一个对象,则必须手动的利用constructor指回原来的构造函数
- constructor: Star,
- sing: function() {
- console.log('我会唱歌');
- },
- movie: function() {
- console.log('我会演电影');
- }
- }
- var ldh = new Star('刘德华', 18);
- var zxy = new Star('张学友', 19);
- console.log(Star.prototype);
- console.log(ldh.__proto__);
- console.log(Star.prototype.constructor);
- console.log(ldh.__proto__.constructor);
- </script>

eg:
- <script>
- // forEach 迭代(遍历) 数组
- var arr = [1, 2, 3];
- var sum = 0;
- arr.forEach(function(value, index, array) {
- console.log('每个数组元素' + value);
- console.log('每个数组元素的索引号' + index);
- console.log('数组本身' + array);
- sum += value;
- })
- console.log(sum);
- </script>
eg:
- <script>
- // filter 筛选数组
- var arr = [12, 66, 4, 88, 3, 7];
- var newArr = arr.filter(function(value, index) {
- // return value >= 20;
- return value % 2 === 0;
- });
- console.log(newArr);
- </script>
eg:
- <script>
- // some 查找数组中是否有满足条件的元素
- // var arr = [10, 30, 4];
- // var flag = arr.some(function(value) {
- // // return value >= 20;
- // return value < 3;
- // });
- // console.log(flag);
- var arr1 = ['red', 'pink', 'blue'];
- var flag1 = arr1.some(function(value) {
- return value == 'pink';
- });
- console.log(flag1);
- // 1. filter 也是查找满足条件的元素 返回的是一个数组 而且是把所有满足条件的元素返回回来
- // 2. some 也是查找满足条件的元素是否存在 返回的是一个布尔值 如果查找到第一个满足条件的元素就终止循环
- </script>

eg:
- <input type="text"> <button>点击</button>
- <div></div>
- <script>
- // trim 方法去除字符串两侧空格
- var str = ' an dy ';
- console.log(str);
- var str1 = str.trim();
- console.log(str1);
- var input = document.querySelector('input');
- var btn = document.querySelector('button');
- var div = document.querySelector('div');
- btn.onclick = function() {
- var str = input.value.trim();
- if (str === '') {
- alert('请输入内容');
- } else {
- console.log(str);
- console.log(str.length);
- div.innerHTML = str;
- }
- }
- </script>

1、keys
eg:
- script>
- // 用于获取对象自身所有的属性
- var obj = {
- id: 1,
- pname: '小米',
- price: 1999,
- num: 2000
- };
- var arr = Object.keys(obj);
- console.log(arr);
- arr.forEach(function(value) {
- console.log(value);
-
- })
- </script>
2、defineProperty
eg:
- <script>
- // Object.defineProperty() 定义新属性或修改原有的属性
- var obj = {
- id: 1,
- pname: '小米',
- price: 1999
- };
- // 1. 以前的对象添加和修改属性的方式
- // obj.num = 1000;
- // obj.price = 99;
- // console.log(obj);
- // 2. Object.defineProperty() 定义新属性或修改原有的属性
- Object.defineProperty(obj, 'num', {
- value: 1000,
- enumerable: true
- });
- console.log(obj);
- Object.defineProperty(obj, 'price', {
- value: 9.9
- });
- console.log(obj);
- Object.defineProperty(obj, 'id', {
- // 如果值为false 不允许修改这个属性值 默认值也是false
- writable: false,
- });
- obj.id = 2;
- console.log(obj);
- Object.defineProperty(obj, 'address', {
- value: '中国山东蓝翔技校xx单元',
- // 如果只为false 不允许修改这个属性值 默认值也是false
- writable: false,
- // enumerable 如果值为false 则不允许遍历, 默认的值是 false
- enumerable: false,
- // configurable 如果为false 则不允许删除这个属性 不允许在修改第三个参数里面的特性 默认为false
- configurable: false
- });
- console.log(obj);
- console.log(Object.keys(obj));
- delete obj.address;
- console.log(obj);
- delete obj.pname;
- console.log(obj);
- Object.defineProperty(obj, 'address', {
- value: '中国山东蓝翔技校xx单元',
- // 如果只为false 不允许修改这个属性值 默认值也是false
- writable: true,
- // enumerable 如果值为false 则不允许遍历, 默认的值是 false
- enumerable: true,
- // configurable 如果为false 则不允许删除这个属性 默认为false
- configurable: true
- });
- console.log(obj.address);
- </script>

eg:
- <script>
- // 函数的定义方式
-
- // 1. 自定义函数(命名函数)
-
- function fn() {};
-
- // 2. 函数表达式 (匿名函数)
-
- var fun = function() {};
-
-
- // 3. 利用 new Function('参数1','参数2', '函数体');
-
- var f = new Function('a', 'b', 'console.log(a + b)');
- f(1, 2);
- // 4. 所有函数都是 Function 的实例(对象)
- console.dir(f);
- // 5. 函数也属于对象
- console.log(f instanceof Object);
- </script>

- <script>
- // 函数的调用方式
-
- // 1. 普通函数
- function fn() {
- console.log('人生的巅峰');
-
- }
- // fn(); fn.call()
- // 2. 对象的方法
- var o = {
- sayHi: function() {
- console.log('人生的巅峰');
-
- }
- }
- o.sayHi();
- // 3. 构造函数
- function Star() {};
- new Star();
- // 4. 绑定事件函数
- // btn.onclick = function() {}; // 点击了按钮就可以调用这个函数
- // 5. 定时器函数
- // setInterval(function() {}, 1000); 这个函数是定时器自动1秒钟调用一次
- // 6. 立即执行函数
- (function() {
- console.log('人生的巅峰');
- })();
- // 立即执行函数是自动调用
- </script>

eg:
- <script>
- // 函数的不同调用方式决定了this 的指向不同
- // 1. 普通函数 this 指向window
- function fn() {
- console.log('普通函数的this' + this);
- }
- window.fn();
- // 2. 对象的方法 this指向的是对象 o
- var o = {
- sayHi: function() {
- console.log('对象方法的this:' + this);
- }
- }
- o.sayHi();
- // 3. 构造函数 this 指向 ldh 这个实例对象 原型对象里面的this 指向的也是 ldh这个实例对象
- function Star() {};
- Star.prototype.sing = function() {
-
- }
- var ldh = new Star();
- // 4. 绑定事件函数 this 指向的是函数的调用者 btn这个按钮对象
- var btn = document.querySelector('button');
- btn.onclick = function() {
- console.log('绑定时间函数的this:' + this);
- };
- // 5. 定时器函数 this 指向的也是window
- window.setTimeout(function() {
- console.log('定时器的this:' + this);
-
- }, 1000);
- // 6. 立即执行函数 this还是指向window
- (function() {
- console.log('立即执行函数的this' + this);
- })();
- </script>

eg
:
- <script>
- // 改变函数内this指向 js提供了三种方法 call() apply() bind()
-
- // 1. call()
- var o = {
- name: 'andy'
- }
-
- function fn(a, b) {
- console.log(this);
- console.log(a + b);
-
- };
- fn.call(o, 1, 2);
- // call 第一个可以调用函数 第二个可以改变函数内的this 指向
- // call 的主要作用可以实现继承
- function Father(uname, age, sex) {
- this.uname = uname;
- this.age = age;
- this.sex = sex;
- }
-
- function Son(uname, age, sex) {
- Father.call(this, uname, age, sex);
- }
- var son = new Son('刘德华', 18, '男');
- console.log(son);
- </script>

eg:
- <script>
- // 改变函数内this指向 js提供了三种方法 call() apply() bind()
-
- // 2. apply() 应用 运用的意思
- var o = {
- name: 'andy'
- };
-
- function fn(arr) {
- console.log(this);
- console.log(arr); // 'pink'
-
- };
- fn.apply(o, ['pink']);
- // 1. 也是调用函数 第二个可以改变函数内部的this指向
- // 2. 但是他的参数必须是数组(伪数组)
- // 3. apply 的主要应用 比如说我们可以利用 apply 借助于数学内置对象求数组最大值
- // Math.max();
- var arr = [1, 66, 3, 99, 4];
- var arr1 = ['red', 'pink'];
- // var max = Math.max.apply(null, arr);
- var max = Math.max.apply(Math, arr);
- var min = Math.min.apply(Math, arr);
- console.log(max, min);
- </script>

- <script>
- // 改变函数内this指向 js提供了三种方法 call() apply() bind()
-
- // 3. bind() 绑定 捆绑的意思
- var o = {
- name: 'andy'
- };
-
- function fn(a, b) {
- console.log(this);
- console.log(a + b);
-
-
- };
- var f = fn.bind(o, 1, 2);
- f();
- // 1. 不会调用原来的函数 可以改变原来函数内部的this 指向
- // 2. 返回的是原函数改变this之后产生的新函数
- // 3. 如果有的函数我们不需要立即调用,但是又想改变这个函数内部的this指向此时用bind
- // 4. 我们有一个按钮,当我们点击了之后,就禁用这个按钮,3秒钟之后开启这个按钮
- // var btn1 = document.querySelector('button');
- // btn1.onclick = function() {
- // this.disabled = true; // 这个this 指向的是 btn 这个按钮
- // // var that = this;
- // setTimeout(function() {
- // // that.disabled = false; // 定时器函数里面的this 指向的是window
- // this.disabled = false; // 此时定时器函数里面的this 指向的是btn
- // }.bind(this), 3000); // 这个this 指向的是btn 这个对象
- // }
- var btns = document.querySelectorAll('button');
- for (var i = 0; i < btns.length; i++) {
- btns[i].onclick = function() {
- this.disabled = true;
- setTimeout(function() {
- this.disabled = false;
- }.bind(this), 2000);
- }
- }
- </script>

call、apply、bind总结
eg;
- <script>
- // 闭包(closure)指有权访问另一个函数作用域中变量的函数。
- // 一个作用域可以访问另外一个函数的局部变量
- // 我们fn 外面的作用域可以访问fn 内部的局部变量
- // 闭包的主要作用: 延伸了变量的作用范围
- function fn() {
- var num = 10;
-
- // function fun() {
- // console.log(num);
-
- // }
- // return fun;
- return function() {
- console.log(num);
- }
- }
- var f = fn();
- f();
- // 类似于
- // var f = function() {
- // console.log(num);
- // }
- // var f = function fun() {
- // console.log(num);
-
- // }
- </script>

eg:
- <script>
- // 递归函数 : 函数内部自己调用自己, 这个函数就是递归函数
- var num = 1;
-
- function fn() {
- console.log('我要打印6句话');
-
- if (num == 6) {
- return; // 递归里面必须加退出条件
- }
- num++;
- fn();
- }
- fn();
- </script>
浅拷贝
- <script>
- // 浅拷贝只是拷贝一层, 更深层次对象级别的只拷贝引用.
- // 深拷贝拷贝多层, 每一级别的数据都会拷贝.
- var obj = {
- id: 1,
- name: 'andy',
- msg: {
- age: 18
- }
- };
- var o = {};
- // for (var k in obj) {
- // // k 是属性名 obj[k] 属性值
- // o[k] = obj[k];
- // }
- // console.log(o);
- // o.msg.age = 20;
- // console.log(obj);
-
- console.log('--------------');
- Object.assign(o, obj);
- console.log(o);
- o.msg.age = 20;
- console.log(obj);
- </script>

深拷贝
- <script>
- // 深拷贝拷贝多层, 每一级别的数据都会拷贝.
- var obj = {
- id: 1,
- name: 'andy',
- msg: {
- age: 18
- },
- color: ['pink', 'red']
- };
- var o = {};
- // 封装函数
- function deepCopy(newobj, oldobj) {
- for (var k in oldobj) {
- // 判断我们的属性值属于那种数据类型
- // 1. 获取属性值 oldobj[k]
- var item = oldobj[k];
- // 2. 判断这个值是否是数组
- if (item instanceof Array) {
- newobj[k] = [];
- deepCopy(newobj[k], item)
- } else if (item instanceof Object) {
- // 3. 判断这个值是否是对象
- newobj[k] = {};
- deepCopy(newobj[k], item)
- } else {
- // 4. 属于简单数据类型
- newobj[k] = item;
- }
-
- }
- }
- deepCopy(o, obj);
- console.log(o);
-
- var arr = [];
- console.log(arr instanceof Object);
- o.msg.age = 20;
- console.log(obj);
- </script>

eg:
- <script>
- // 正则表达式在js中的使用
-
- // 1. 利用 RegExp对象来创建 正则表达式
- var regexp = new RegExp(/123/);
- console.log(regexp);
-
- // 2. 利用字面量创建 正则表达式
- var rg = /123/;
- // 3.test 方法用来检测字符串是否符合正则表达式要求的规范
- console.log(rg.test(123));
- console.log(rg.test('abc'));
- </script>
- <script>
- //var rg = /abc/; 只要包含abc就可以
- // 字符类: [] 表示有一系列字符可供选择,只要匹配其中一个就可以了
- var rg = /[abc]/; // 只要包含有a 或者 包含有b 或者包含有c 都返回为true
- console.log(rg.test('andy'));
- console.log(rg.test('baby'));
- console.log(rg.test('color'));
- console.log(rg.test('red'));
- var rg1 = /^[abc]$/; // 三选一 只有是a 或者是 b 或者是c 这三个字母才返回 true
- console.log(rg1.test('aa'));
- console.log(rg1.test('a'));
- console.log(rg1.test('b'));
- console.log(rg1.test('c'));
- console.log(rg1.test('abc'));
- console.log('------------------');
-
- var reg = /^[a-z]$/; // 26个英文字母任何一个字母返回 true - 表示的是a 到z 的范围
- console.log(reg.test('a'));
- console.log(reg.test('z'));
- console.log(reg.test(1));
- console.log(reg.test('A'));
- // 字符组合
- var reg1 = /^[a-zA-Z0-9_-]$/; // 26个英文字母(大写和小写都可以)任何一个字母返回 true
- console.log(reg1.test('a'));
- console.log(reg1.test('B'));
- console.log(reg1.test(8));
- console.log(reg1.test('-'));
- console.log(reg1.test('_'));
- console.log(reg1.test('!'));
- console.log('----------------');
- // 如果中括号里面有^ 表示取反的意思 千万和 我们边界符 ^ 别混淆
- var reg2 = /^[^a-zA-Z0-9_-]$/;
- console.log(reg2.test('a'));
- console.log(reg2.test('B'));
- console.log(reg2.test(8));
- console.log(reg2.test('-'));
- console.log(reg2.test('_'));
- console.log(reg2.test('!'));
- </script>

eg:
- <script>
- // 量词符: 用来设定某个模式出现的次数
- // var reg = /^a$/;
- // console.log(reg.test('a'));
- // console.log(reg.test('aa'));
-
-
- // 1. * 相当于 >= 0 可以出现0次或者很多次
- // var reg = /^a*$/;
- // console.log(reg.test(''));
- // console.log(reg.test('a'));
- // console.log(reg.test('aa'));
- // console.log(reg.test('aaaaaa'));
-
- // 2. + 相当于 >= 1 可以出现1次或者很多次
- // var reg = /^a+$/;
- // console.log(reg.test(''));
- // console.log(reg.test('a'));
- // console.log(reg.test('aa'));
- // console.log(reg.test('aaaaaa'));
- // 3. ? 相当于 1 || 0
- // var reg = /^a?$/;
- // console.log(reg.test(''));
- // console.log(reg.test('a'));
- // console.log(reg.test('aa'));
- // console.log(reg.test('aaaaaa'));
- // 4. {3 } 就是重复3次
- // var reg = /^a{3}$/;
- // console.log(reg.test(''));
- // console.log(reg.test('a'));
- // console.log(reg.test('aa'));
- // console.log(reg.test('aaaaaa'));
- // console.log(reg.test('aaa'));
- // 5. {3, } 大于等于3
- var reg = /^a{3,}$/;
- console.log(reg.test(''));
- console.log(reg.test('a'));
- console.log(reg.test('aa'));
- console.log(reg.test('aaaaaa'));
- console.log(reg.test('aaa'));
- // 6. {3, 16} 大于等于3 并且 小于等于16
-
- var reg = /^a{3,16}$/;
- console.log(reg.test(''));
- console.log(reg.test('a'));
- console.log(reg.test('aa'));
- console.log(reg.test('aaaaaa'));
- console.log(reg.test('aaa'));
- console.log(reg.test('aaaaaaaaaaaaaaaaaaaaa'));
- // 量词是设定某个模式出现的次数
- var reg = /^[a-zA-Z0-9_-]{6,16}$/; // 这个模式用户只能输入英文字母 数字 下划线 短横线但是有边界符和[] 这就限定了只能多选1
- // {6,16} 中间不要有空格
- // console.log(reg.test('a'));
- // console.log(reg.test('8'));
- // console.log(reg.test('18'));
- // console.log(reg.test('aa'));
- // console.log('-------------');
- // console.log(reg.test('andy-red'));
- // console.log(reg.test('andy_red'));
- // console.log(reg.test('andy007'));
- // console.log(reg.test('andy!007'));
- </script>

- <script>
- // 中括号 字符集合.匹配方括号中的任意字符.
- // var reg = /^[abc]$/;
- // a 也可以 b 也可以 c 可以 a ||b || c
- // 大括号 量词符. 里面表示重复次数
- // var reg = /^abc{3}$/; // 它只是让c重复三次 abccc
- // console.log(reg.test('abc'));
- // console.log(reg.test('abcabcabc'));
- // console.log(reg.test('abccc'));
-
- // 小括号 表示优先级
- var reg = /^(abc){3}$/; // 它是让abcc重复三次
- console.log(reg.test('abc'));
- console.log(reg.test('abcabcabc'));
- console.log(reg.test('abccc'));
- </script>

- <textarea name="" id="message"></textarea> <button>提交</button>
- <div></div>
- <script>
- // 替换 replace
- // var str = 'andy和red';
- // // var newStr = str.replace('andy', 'baby');
- // var newStr = str.replace(/andy/, 'baby');
- // console.log(newStr);
- var text = document.querySelector('textarea');
- var btn = document.querySelector('button');
- var div = document.querySelector('div');
- btn.onclick = function() {
- div.innerHTML = text.value.replace(/激情|gay/g, '**');
- }
- </script>
注:对应的内存地址不能更改
eg:
- <script type="text/javascript">
- // 数组解构允许我们按照一一对应的关系从数组中提取值 然后将值赋值给变量
- let ary = [1,2,3];
- let [a, b, c, d, e] = ary;
- console.log(a)
- console.log(b)
- console.log(c)
- console.log(d)
- console.log(e)
- </script>
第一种:
第二种:
eg:
- <script type="text/javascript">
- // 对象解构允许我们使用变量的名字匹配对象的属性 匹配成功 将对象属性的值赋值给变量
-
- let person = {name: 'lisi', age: 30, sex: '男'};
- // let { name, age, sex } = person;
- // console.log(name)
- // console.log(age)
- // console.log(sex)
-
- let {name: myName} = person;
- console.log(myName)
-
- </script>
- <script type="text/javascript">
- 箭头函数是用来简化函数定义语法的
- const fn = () => {
- console.log(123)
- }
- fn();
-
- 在箭头函数中 如果函数体中只有一句代码 并且代码的执行结果就是函数的返回值 函数体大括号可以省略
- const sum = (n1, n2) => n1 + n2;
- const result = sum(10, 20);
- console.log(result)
-
- 在箭头函数中 如果形参只有一个 形参外侧的小括号也是可以省略的
- const fn = v => {
- alert(v);
- }
- fn(20)
-
- </script>

- 头函数不绑定this 箭头函数没有自己的this关键字 如果在箭头函数中使用this this关键字将指向箭头函数定义位置中的this
-
- function fn () {
- console.log(this);
- return () => {
- console.log(this)
- }
- }
-
- const obj = {name: 'zhangsan'};
-
- const resFn = fn.call(obj);
-
- resFn();
1合并数组
- 扩展运算符应用于数组合并
- let ary1 = [1, 2, 3];
- let ary2 = [4, 5, 6];
- // ...ary1 // 1, 2, 3
- // ...ary1 // 4, 5, 6
- let ary3 = [...ary1, ...ary2];
- console.log(ary3)
-
- 合并数组的第二种方法
- let ary1 = [1, 2, 3];
- let ary2 = [4, 5, 6];
-
- ary1.push(...ary2);
- console.log(ary1)
2.将类数组或可遍历对象转换为真正得到数组
eg:
- // 利用扩展运算符将伪数组转换为真正的数组
- var oDivs = document.getElementsByTagName('div');
- console.log(oDivs)
- var ary = [...oDivs];
- ary.push('a');
- console.log(ary);
1.将类数组或可遍历对象转换为真正得到数组
2.接受第二个参数,作为类似数组map方法,用于对每个元素处理后放入返回数组
-
- //模板字符串(ES6)的使用
- //语法:var str = `es6的字符串`; 反单引号表示
- //用${}在字符串中嵌入变量表达式,
- var username = "张三";
- var str1 = `我的名字是${username}`;
- var str2 = `1+1的结果是${1+1}`;
- document.write(str1); //输出结果为我的名字是username
- document.write(str2); //输出结果为1+1的结果为2
1、startsWith()和endsWith()
2、repeat()
- const s1 = new Set();
- console.log(s1.size)//0
-
- const s2 = new Set(["a", "b"]);
- console.log(s2.size)//2
- // 去重
- const s3 = new Set(["a","a","b","b"]);
- console.log(s3.size)//2
- const ary = [...s3];
- console.log(ary)//含a,b的数组
Set数据结构方法
eg:
- const s4 = new Set();
- // 向set结构中添加值 使用add方法
- s4.add('a').add('b');
- console.log(s4.size)
-
- // 从set结构中删除值 用到的方法是delete
- const r1 = s4.delete('c');
- console.log(s4.size)
- console.log(r1);
-
- // 判断某一个值是否是set数据结构中的成员 使用has
- const r2 = s4.has('d');
- console.log(r2)
-
- // 清空set数据结构中的值 使用clear方法
- s4.clear();
- console.log(s4.size);
-

- // 遍历set数据结构 从中取值
- const s5 = new Set(['a', 'b', 'c']);
- s5.forEach(value => {
- console.log(value)
- })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。