12.javascript_高级
1. function调用
abc();
abc(1);
abc(1, 2)
abc(1, 2, 3)
function abc(a, b, c) {
console.log(a + "\r\n" + b);
for (let i = 0; i < arguments.length; i++) {
console.info(arguments[i])
}
}
2. hasOwnProperty:判断是否有属性
-
hasOwnProperty: 判断是否有属性(方法); 继承的不行
function Student() { } var stu1 = new Student(); stu1.name = "tom"; // 自由组装属性 stu1.age = 10; stu1.setter=function(e, x) { this.name = e; this.age = x; } stu1.setter("jerry", 20) console.info(stu1.name + "@" + stu1.age) console.info(stu1.hasOwnProperty) console.info(stu1.hasOwnProperty("setter")) var stu2 = new Student(); console.info(stu2.hasOwnProperty("name")) console.info(stu1.toString()) console.info(stu1.hasOwnProperty("toString")) // ??
3. 继承
封装: 通过闭包属性私有化。
继承?????
-
原型链继承:
-
类名.prototype.属性=“值”; 通过原型链扩展属性;
-
类名.prototype=new 父类(); 模拟原型链继承;
function A() { } var a = new A(); console.info(a.hasOwnProperty("name")) a.name = "张三"; console.info(a.hasOwnProperty("name")) A.prototype.ename = "hello"; // // a.ename="李四" console.info(a.ename); // 取值,如果属性没有就会取原型链上找直到Object ------------ function Animal() { this.name= "aaa"; } function Dog() { this.age = 10; } // 修改了原型链 Dog -> Animal -> Object Dog.prototype = new Animal(); console.info(Dog.prototype) var d = new Dog(); console.info(d.age) console.info(d.name)
-
-
构造器继承:
利用call/apply/bind方法替换作用域的this
function A(pname) {
this.name = pname;
}
function B(sname) {
this.age = 100
// 方法调用,替换作用域
A.call(this, sname);
}
var a1 = new B("jerry");
console.info(a1.age + "@" + a1.name)
//
var obj1 = {ename: "tom", getEname(){return this.ename}}
console.info(obj1.getEname());
var obj2 = {ename: "jerry"}
console.info(obj1.getEname.call(obj2)) // obj1.getEname() getEname(){return obj2.ename}
4. parseInt()方法
-
parseInt(“100”); 类型转换
-
parseInt(“00100”); 100
-
parseInt(“1.12”); 1
-
parseInt(“abc”); NaN
-
parseInt(“123abc566”); 123
var n = parseInt("abc"); console.info(n); // NaN var n = parseInt("123abc567"); console.info(n); // 123 从左面有效的拿出来
5. parseFloat()方法
nsole.info(n); // NaN
var n = parseInt(“123abc567”);
console.info(n); // 123 从左面有效的拿出来
# 5. parseFloat()方法