面向物件的特性 封裝 和 繼承
子類strudent 繼承了父類 Person的屬性
// 父類
function Person(name, height) {
this.name = name;
this.height = height;
}
Person.prototype.say = function () {
console.log(this.name);
console.log(this.height);
}
// 子類
function Student(grade, name, height) {
// 借用了父類的建構式,完成對自己的賦值
Person.call(this, name, height)
this.grade = grade;
}
// 賦值了父類原型上的所有的 屬性和方法
Student.prototype = Person.prototype;
// 修改之類的指向
Student.prototype.constructor = Student;
// 創建子類的實體
const stu = new Student("兩年", "海海吶", 180);
stu.say();
函式引數默認值
定義函式的同時,可以給形參一個默認值
// 定義函式的同時,可以給形參一個默認值
function show(msg = "你好呀") {
console.log(msg);
}show(); // 你好呀
show("你好,再見"); // 你好,再見
物件簡寫
const name = "李白";
const skill = "大河之劍??";
const say = function () { }
const obj = {
name, skill, say
}
console.log(obj);// {name:"李白",skill:"大河之劍??",say:function(){}}
解構
提供更加方便獲取陣列中元素或者物件中屬性的寫法
獲取陣列中的元素
const [a, b, c, d] = [1, 2, 3, 4];
console.log(a, b, c, d); // 1,2,3,4
元素互動順序
let a = 1111;
let b = 2222;
[b, a] = [a, b];
console.log(a, b); // 2222 1111
獲取物件中的屬性
const obj = {
name: "李白",
skill: "大河之劍???",
say() { }
}
const { name, skill,say } = obj;
console.log(name, skill,say); // 李白 大河之劍??? function(){}
拓展運算子 || 剩余運算子
通過 ... 符號來獲取剩下的引數
函式內獲取
function show(a, ...all) {
console.log(a);
console.log(all);
}
show(1);// 1 []
show(1, 2, 3); // 1 [2,3]
陣列內獲取
const [a, ...rest] = [1, 2, 3, 4, 5];
console.log(a); // 1
console.log(rest);// [2, 3, 4, 5]
物件內獲取
const obj={
name:"海海",
age:"18",
say(){}
}const {name,...others}=obj;
console.log(name); // 海海
console.log(others); // {age: "18", say: ?}
上一章:JavaScript 進階第八章(閉包)
下一章:JavaScript 進階第十章(遞回)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/297163.html
標籤:其他
