apply想括、call 鉴腻、bind有什么作用忠寻,什么區(qū)別
fn.call(context, param1, param2...)
fn.apply(context, paramArray)
fn.bind(context)
以下代碼輸出什么?
var john = {
firstName: "John"
}
function func() {
alert(this.firstName + ": hi!")
}
john.sayHi = func
john.sayHi()
輸出:John: hi!
下面代碼輸出什么惧浴,為什么
func()
function func() {
alert(this)
}
輸出:[object Window]
下面代碼輸出什么
document.addEventListener('click', function(e){
console.log(this);
setTimeout(function(){
console.log(this);
}, 200);
}, false);
//點擊后,先輸出document,再輸出window奕剃;(setTimeout/setInterval中的this均指向window)
下面代碼輸出什么衷旅,why
var john = {
firstName: "John"
}
function func() {
alert( this.firstName )
}
func.call(john)
//輸出John因為call改變了函數的this指針
以下代碼有什么問題,如何修改
var module= {
bind: function(){
$btn.on('click', function(){
console.log(this) //this指什么
this.showMsg();
})
},
showMsg: function(){
console.log('饑人谷');
}
}
由于事件觸發(fā)中纵朋,this代表了$btn這個DOM節(jié)點柿顶,而不是程序員所期望的module對象,所以輸出無效操软;
正確的應提前綁定this嘁锯,改正后:
var module= {
bind: function(){
var that = this;
$btn.on('click', function(){
that.showMsg();
})
},
showMsg: function(){
console.log('饑人谷');
}
}
module.bind();
有如下代碼,解釋Person聂薪、 prototype家乘、proto、p藏澳、constructor之間的關聯烤低。
function Person(name){
this.name = name;
}
Person.prototype.sayName = function(){
console.log('My name is :' + this.name);
}
var p = new Person("若愚")
p.sayName();
上一題中,對對象 p我們可以這樣調用 p.toString()笆载。toString是哪里來的? 畫出原型圖?并解釋什么是原型鏈
toString()是Person.prototype里面的方法。p在調用方法時先從自身找再從proto里面找涯呻,找不到就從proto.proto里面找凉驻。
對String做擴展,實現如下方式獲取字符串中頻率最高的字符
String.prototype.getMostOften = function() {
var h = {};
var maxNum = 0;
var maxEle = null;
for(let i = 0; i<this.length; i++) {
var a = this[i];
h[a] === undefined ? h[a]=1 : (h[a]++);
if(h[a] > maxNum) {
maxNum = h[a];
maxEle = a;
}
}
return maxEle;
};
var str = 'ahbbccdeddddfg';
var ch = str.getMostOften();
console.log(ch); //d , 因為d 出現了5次
instanceOf有什么作用复罐?內部邏輯是如何實現的涝登?
instanceOf 用于判斷當前引用類型對象是不是某個構造函數的實例。例如
var a=[1,2,3,4],
b={"acorn":1,"z-one":2},
c="acorn";
a instanceof Array //true
b instanceof Array //false
b instanceof Object //true
c instanceof Array //false 對象C不是引用類型效诅,
其實 instanceOf 內部邏輯就是判斷 當前引用類型對象的proto 與 目標對象的prototype是否為同一個胀滚。從而判斷是否為當前對象的實例.
代碼如下
function show(obj,node){
if(obj.__proto__===node.prototype){
return true;
}else{
return false;
}
}
var a=[1,2,3,4]
show(a,Array)//true
這里我們發(fā)現可以趟济。但是如果遇到這種情況?
function abc(){
this.name=[1,2,3,5,6]
}
var p=new abc();
var c=p.name;
show(c.Objecet)//false
這里卻為 false.這是什么原因?
insranceOf 正確判斷邏輯
inStanceOf 內部邏輯判斷為:先判斷當前引用對象的proto是否和目標構造函數的prototype相等咽笼,如果不相等在判斷引用對象的proto.proto 是否相等
function show(obj,node){
if(obj.__proto__===node.prototype){
return true;
}else if(obj.__proto__.__proto__===node.prototype){
return true;
}else return false;
}
下面兩種寫法有什么區(qū)別?
//方法1
function People(name, sex){
this.name = name;
this.sex = sex;
this.printName = function(){
console.log(this.name);
}
}
var p1 = new People('饑人谷', 2)
//方法2
function Person(name, sex){
this.name = name;
this.sex = sex;
}
Person.prototype.printName = function(){
console.log(this.name);
}
var p1 = new Person('若愚', 2);
第一個例子里在p1這個實例里有printName這個方法顷编,第二個例子里printName方法被放在p1的prototype里。
Object.create 有什么作用剑刑?
Object.create()方法創(chuàng)建一個新對象媳纬,使用現有的對象來提供新創(chuàng)建的對象的proto。
返回值:一個新對象施掏,帶著指定的原型對象和屬性钮惠。
用 Object.create
實現類式繼承
// Shape - 父類(superclass)
function Shape() {
this.x = 0;
this.y = 0;
}
// 父類的方法
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.info('Shape moved.');
};
// Rectangle - 子類(subclass)
function Rectangle() {
Shape.call(this); // call super constructor.
}
// 子類續(xù)承父類
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var rect = new Rectangle();
console.log('Is rect an instance of Rectangle?',
rect instanceof Rectangle); // true
console.log('Is rect an instance of Shape?',
rect instanceof Shape); // true
rect.move(1, 1); // Outputs, 'Shape moved.'
hasOwnProperty有什么作用?給出范例
hasOwnProperty() 方法會返回一個布爾值七芭,指示對象自身屬性中是否具有指定的屬性素挽。
如下代碼中call的作用是什么?
function Person(name, sex){
this.name = name;
this.sex = sex;
}
function Male(name, sex, age){
Person.call(this, name, sex); //這里的 call 有什么作用
this.age = age;
}
call 調用Person函數 第一個參數this指向Male,使Male函數實現構造函數繼承狸驳。
補全代碼预明,實現繼承
function Person(name, sex){
// todo ...
}
Person.prototype.getName = function(){
// todo ...
};
function Male(name, sex, age){
//todo ...
}
//todo ...
Male.prototype.getAge = function(){
//todo ...
};
var hugner = new Male('饑人谷', '男', 2);
hugner.printName();
function Person(name, sex){
this.name = name;
this.sex = sex;
}
Person.prototype.getName = function(){
console.log('Person name is '+ this.name);
};
function Male(name, sex, age){
Person.call(this, name, sex);
this.age = age;
}
// 方法1
Male.prototype = new Person();
// 方法2
// Male.prototype = Object.create(Person.prototype)
Male.prototype.printName = function(){
console.log(this.name);
}
Male.prototype.getAge = function(){
console.log(this.name +'--'+ this.age);
};
var ruoyu = new Male('若愚', '男', 27);
var jirengu = new Person('jirengu', '男', 3);
ruoyu.printName();
ruoyu.getAge();
jirengu.getName();