前端面试总结——原型和原型链

1.class

class People {
  constructor(name)
  {this.name=name;}
  eat(){
    console.log(this.name+'is eating something')
  }
}
const zhangsan=new People(张三);
console.log(zhangsan.name);
zhangsan.eat();
class Student extends People{
constructor(name,number){
   super(name);
   this.number=number;
}
study(){
console.log(this.name+'is learning');
  }
}
const lihua=new Student(李华,100);
console.log(lihua.name);
console.log(lihua.number);
lihua.eat();
lihua.study();

2.原型关系

每个class都有显式原型prototype

每个实例都有隐式原型_proto_

实例的_proto_指向对应class的显式原型

class.prototype===example._proto_

3.基于原型的执行规则

获取属性和方法时,

先在对象本身的方法和属性里查找

如果没有找到,就到对象的_proto_中查找

4.手写简易的jQuery

class jQury{
      constructor(selector){
        let result=document.querySelectorAll(selector);
        let len=result.length;
        this.length=len;
        for(let i=0;i<len;i++ ){
         this[i]=result[i];}
     }
     get(i){ return this[i];}
     each(fn){
       for(let i=0;i<this.length;i++){
       let elem=this[i];
       fn(elem);} 
     }
     on(type,fn){
     return this.each((elem)=>{
     elem.addeventListener(type,fn,false);
})
}
}

5.手写代码

创建10个a标签,点击的时候弹出对应的序列号

let a;
for(let i=0;i<10;i++){
a=document.creatElement('a');
a.innerHtml=i+'<br>'
a.addEventListener('click',function(e){
         e.preventDeafault()
         alert(i)
})
document.body.appendChild(a);
}

6.this

fn1(){ console.log(this);}

fn1();//this指向window

fn1().call({x:100});//{x:100}

const fn2=fn1.bind({x:200});

fn2();//{x:200}

const zhangsan={
  name:'张三',
  sayhi(){
  console.log(this);//zhangsan},
  wait(){settimeout(
  function{console.log(this);//window}
  )}
}

const zhangsan={
 name:'张三',
sayhi(){
console.log(this);//zhangsan},
waitagain(){
settimeout(()=>{
console.log(this);//zhangsan
})
}
}

class People{
constructor(name){
this.name=name;
this.age=age;
}
sayhi(){
console.log(this);
}
}
const zhangsan=new People('张三');
zhangsan.sayhi();//zhangsan对象

总结:

1.当作普通函数被调用,this指向window

2.使用call,apply,bind,this指向call,apply,bind所绑定的对象

3.作为对象的方法调用,this指向该对象

4.在class的方法中调用,this指向class创建的实例

5.箭头函数的this指向上级作用域的this

7.手写bind

Function.prototype.mybind=function(){
let args=Array.prototype.slice.call(arguments);
const t=args.shift();
const self=this;
return function(){
return self.apply(t,args);
}
}

8.闭包的实际应用

//闭包隐藏数据,只提供API
function createCahe(){
  const data={};//data中的数据,不被外界访问
return{
set:function(key,value){
  data[key]=value;
}
get:function(key){
  return data[key];
    }
  }
}

const c=createCahe();
c.set('a',100);
c.get('a');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
原型链是JavaScript中非常重要的一个概念,它是实现继承的主要方法之一。在JavaScript中,每个对象都有一个原型对象,而原型对象也可以有自己的原型对象,这样就形成了一个原型链。当我们访问一个对象的属性或方法时,如果该对象本身没有这个属性或方法,就会去它的原型对象中查找,如果还没有,就会去原型对象的原型对象中查找,直到找到该属性或方法或者到达原型链的顶端为止。 下是一个简单的例子,演示了原型链的基本概念: ```javascript function Person(name, age) { this.name = name; this.age = age; } Person.prototype.sayHello = function() { console.log("Hello, my name is " + this.name); }; function Student(name, age, grade) { Person.call(this, name, age); this.grade = grade; } Student.prototype = Object.create(Person.prototype); Student.prototype.constructor = Student; Student.prototype.sayGrade = function() { console.log("My grade is " + this.grade); }; var student = new Student("Tom", 18, 3); student.sayHello(); // 输出:Hello, my name is Tom student.sayGrade(); // 输出:My grade is 3 ``` 在上的例子中,我们定义了一个`Person`构造函数和一个`Student`构造函数,`Student`构造函数继承自`Person`构造函数。我们通过`Object.create`方法将`Student`的原型对象指向`Person`的原型对象,这样就形成了一个原型链。当我们调用`student.sayHello()`方法时,由于`student`对象本身没有`sayHello`方法,就会去它的原型对象`Person.prototype`中查找,找到了该方法并执行。当我们调用`student.sayGrade()`方法时,由于`student`对象本身没有`sayGrade`方法,就会去它的原型对象`Student.prototype`中查找,找到了该方法并执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值