this 相關問題
問題1: apply、call 、bind有什么作用,什么區(qū)別
apply() 方法調用一個函數(shù), 其具有一個指定的this值,以及作為一個數(shù)組(或類似數(shù)組的對象)提供的參數(shù)。
fun.apply(thisArg, [argsArray])
call() 方法調用一個函數(shù), 其具有一個指定的this值和分別地提供的參數(shù)(參數(shù)的列表)。
fun.call(thisArg[, arg1[, arg2[, ...]]])
使用call方法調用函數(shù)并且指定上下文的'this'bind,返回一個新函數(shù),并且使函數(shù)內部的this為傳入的第一個參數(shù)。bind() 最簡單的用法是創(chuàng)建一個函數(shù),使這個函數(shù)不論怎么調用都有同樣的 this 值。還可以配合setTimeout。
fun.bind(thisArg[, arg1[, arg2[, ...]]])
apply()與 call()非常相似,不同之處在于提供參數(shù)的方式。就是call()方法接受的是若干個參數(shù)的列表,而apply()方法接受的是一個包含多個參數(shù)的數(shù)組。
問題2: 以下代碼輸出什么?
var john = {
firstName: "John"
}
function func() {
alert(this.firstName + ": hi!")
}
john.sayHi = func
john.sayHi()//John: hi!,john.sayHi調用了func,this指向john
問題3: 下面代碼輸出什么,為什么
func()
function func() {
alert(this)
}//[object Window],在函數(shù)被直接調用時this綁定到全局對象。在瀏覽器中,window 就是該全局對象。
問題4:下面代碼輸出什么
document.addEventListener('click', function(e){
console.log(this);
setTimeout(function(){
console.log(this);
}, 200);
}, false);
//ducument
window
問題5:下面代碼輸出什么,why
var john = {
firstName: "John"
}
function func() {
alert( this.firstName )
}
func.call(john)//John,對象john調用了func
問題6: 以下代碼有什么問題,如何修改
var module= {
bind: function(){
$btn.on('click', function(){
console.log(this) //this指$btn
this.showMsg();
})
},
showMsg: function(){
console.log('饑人谷');
}
}
修改后:
var module= {
bind: function(){
let self = this
$btn.on('click', function(){
console.log(self)
this.showMsg();
})
},
showMsg: function(){
console.log('饑人谷');
}
}
原型鏈相關問題
問題7:有如下代碼,解釋Person、 prototype、proto、p、constructor之間的關聯(lián)。
function Person(name){
this.name = name;
}
Person.prototype.sayName = function(){
console.log('My name is :' + this.name);
}
var p = new Person("若愚")
p.sayName();
Person是構造函數(shù)constructor,new Person("若愚")創(chuàng)建了Person的一個對象p
Person擁有屬性prototype
其中Person的實例p的屬性proto指向prototype
prototype的constructor指向Person
問題8: 上例中,對對象 p可以這樣調用 p.toString()。toString是哪里來的? 畫出原型圖?并解釋什么是原型鏈。
p的prototype中沒有toString,就沿著原型鏈,proto指向Person的prototype,其中也沒有toString,沿著原型鏈繼續(xù)指向Object的prototype中找到toString。
每個對象都有一個私有屬性(稱為是[[Prototype]]), 它持有一個連接到另一個稱為其 prototype 對象的鏈接。該原型對象具有一個自己的原型,等等,直到達到一個對象的 prototype 為 null。
根據(jù)定義,null 沒有 prototype,并作為這個 prototype chain 中的最后一個環(huán)節(jié)。
問題9:對String做擴展,實現(xiàn)如下方式獲取字符串中頻率最高的字符
String.prototype.getMostOften = function(){
var dict={}
for(var i=0;i<str.length;i++){
if(dict[str[i]]){
++dict[str[i]]
}else{
dict[str[i]]=1;
}
}
var count=0
var maxValue
for(key in dict){
if(dict[key]>count){
maxValue=key
count=dict[key]
}
}
return maxValue+':'+count
}
var str = 'ahbbccdeddddfg';
var ch = str.getMostOften();
console.log(ch); //d , 因為d 出現(xiàn)了5次
問題10: instanceOf有什么作用?內部邏輯是如何實現(xiàn)的?
instanceof 運算符用來測試一個對象在其原型鏈中是否存在一個構造函數(shù)的 prototype 屬性。
object instanceof constructor
function p(){
console.log('hi')
}
var p1 = new p()
p1 instanceof Object
p1的proto指向p的prototype,p的prototype的proto指向Object的prototype,它們在原型鏈上。
繼承相關問題
問題11:繼承有什么作用?
繼承是指一個對象直接使用另一對象的屬性和方法,可以使得子類別具有父類別的各種屬性和方法。使代碼更簡潔,節(jié)約了內存空間。
問題12: 下面兩種寫法有什么區(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('若愚', 27);
方法二定義的printName方法在prototype原型上,實例可以共用,節(jié)約內存。
方法一定義的printName方法不在原型上,其余實例要用則要再次定義。
問題13: Object.create 有什么作用?兼容性如何?
Object.create(proto, [ propertiesObject ])方法獲取,clone了一個新的prototype。
Object.create是ES5方法,之前版本通過遍歷屬性也可以實現(xiàn)淺拷貝。
問題14: hasOwnProperty有什么作用? 如何使用?
hasOwnPerperty是Object.prototype的一個方法,可以判斷一個對象是否包含自定義屬性而不是原型鏈上的屬性,hasOwnProperty是JavaScript中唯一一個處理屬性但是不查找原型鏈的函數(shù)。
m.hasOwnProperty('name'); // true
m.hasOwnProperty('printName'); // false
Male.prototype.hasOwnProperty('printAge'); // true
問題15:如下代碼中call的作用是什么?
function Person(name, sex){
this.name = name;
this.sex = sex;
}
function Male(name, sex, age){
Person.call(this, name, sex); //指定了上下文的this值,使Male繼承Person的name和sex屬性
this.age = age;
}
問題16: 補全代碼,實現(xiàn)繼承
function Person(name, sex){
this.name = name
this.sex = sex // todo ...
}
Person.prototype.getName = function(){
console.log(this.name)// todo ...
};
function Male(name, sex, age){
Person.call(this, name, sex)
this.age = age//todo ...
}
Male.prototype = Object.create(Person.prototype) //todo ...
Male.prototype.getAge = function(){
console.log(this.age)//todo ...
};
var ruoyu = new Male('若愚', '男', 27);
ruoyu.getName();
ruoyu.getAge();