apply、call 、bind有什么作用,什么区别
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('饥人谷');
}
}
由于事件触发中,this代表了$btn这个DOM节点,而不是程序员所期望的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
这里我们发现可以。但是如果遇到这种情况?
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;
}
下面两种写法有什么区别?
//方法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()方法创建一个新对象,使用现有的对象来提供新创建的对象的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.
}
// 子类续承父类
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();