热门课程

免费试听

上课方式

开班时间

当前位置: 首页 -   文章 -   新闻动态 -   正文

2021年web前端经典面试题及详细解答

知了堂姐
2024-07-09 11:12:24
0
        有非常多前端小伙伴反馈在面试中经常遇到一些经典的前端面试题,其中有不少小伙伴对web前端面试题做了汇总以及详细解答,今天这份《2021年web前端经典面试题及详细答案》就分享给各位小知了。
 
        一javascript: 、javascript:
1.通过写出函数检测JavaScript中一个变量是一个String类型
typeof(obj) === "string"
typeof obj === "string"
obj.constructor === String
 web前端
2.字符串的空格用js去除
方法一使用replace正规匹配
 
去除所有空格: str = str.replace(/s*/g,"");
去除两头空格: str = str.replace(/^s*|s*$/g,"");
去除左空格: str = str.replace( /^s*/, “”);
去除右空格: str = str.replace(/(s*$)/g, "");
str为要去除空格的字符串,实例如下:
var str = " 23 23 ";
var str2 = str.replace(/s*/g,"");
console.log(str2); // 2323
 
方法二:使用str.trim()方法
 
var str = "   xiao  ming   ";
var str2 = str.trim();
console.log(str2);   //xiao  ming
同理,str.trimLeft(),str.trimRight()分别用于去除字符串左右空格。
 
方法三:使用jquery,$.trim(str)方法
 
var str = "   xiao  ming   ";
var str2 = $.trim(str)
console.log(str2);   //  xiao  ming

        3.javascript面向对象中继承实现?

2021年web前端经典面试题及详细解答

       封闭、继承、多态是面向对象的基本特征,那么在Javascript中实现继承有以下方法:
A.原型链(prototype chaining)
function car(price){
    this.price = price;
}
car.prototype.sayPrice = function(){
    console.log("Price is "+this.price);
}
var oCar = new car("100W");
oCar.sayPrice();
 
function toyCar(price){
    this.price = price;
}
toyCar.prototype = new car()
var oCar2 = new toyCar("10CNY");
oCar2.sayPrice();
 
B.call()/apply()
function useCall(a,b){
  this.a = a;
  this.b = b;
  this.say = function(){
      console.log("I'm "+this.a+" You're "+this.b);
  }
 }
 function callThefunction (){
    var args = arguments;
    useCall.call(this,args[0],args[1]);
   // useCall.apply(this,arguments);
  }
var testCall1 = new useCall("Not YY","Not TT");
testCall1.say();
 
var testCall2 = new callThefunction("YY","TT");
testCall2.say();
//  I'm Not YY You're Not TT
//  I'm YY You're TT
 
 
2021年web前端经典面试题及详细解答
C.混合方式(prototype和call()/apply()结合)
function house(size,price){
    this.size = size;
    this.price = price;
}
house.prototype.showArea=function (){
    console.log("面积为"+this.size);
}
house.prototype.sayPrice=function (){
    console.log("价钱为"+this.price);
}
 
function maofan(size,price){
    house.call(this,size,price);
}
maofan.prototype = new house();
 
var newmaofan = new maofan("20Square meters ","1000CNY");
newmaofan.showArea();
 
D.对象冒充
function Person(name,age){
   this.name = name;
   this.age = age;
   this.show = function(){
       console.log(this.name+",  "+this.age);
   }
}
Person.prototype.sayHi = function(){
   alert('hi');
}
 
function Student(name,age){
   this.student = Person;   //将Person类的构造函数赋值给this.student
   this.student(name,age);  //js中实际上是通过对象冒充来实现继承的
   delete this.student;    //移除对Person的引用
}
 
var s = new Student("小明",17);
s.show();
 
var p = new Person("小花",18);
p.show();
// 小明,  17
// 小花,  18
 
大家都在看

喜报 | 考试通过率98.5%!NISP一级20...

2024-07-09 浏览次数:0

2023应届生小姐姐就业成功,入职等保测评工程师

2024-07-09 浏览次数:0

IT专业的你,一定要来天府软件园

2024-07-09 浏览次数:0

新班一周,我们被学员“告白”了!

2024-07-09 浏览次数:0

知了堂应邀参与四川省计算机学会学术年会

2024-07-09 浏览次数:0

软件开发工程师需要学什么?软件开发一般月薪多少

2024-07-09 浏览次数:0
最新资讯
web前端开发和java开发哪...   IT行业在国内的发展形势已经是非常不错了,程序员比比皆是,那么尾巴前端开发和Java开发哪个好?...
web前端框架哪个好?前端三大...   Web前端框架已经是职场必备的了,一个功能强大的框架可以大大提升技术员的工作效率。Web前端框架...
学web前端开发需要什么基础?...   随着全球信息化的告诉发展,移动互联网也得到了高速的发展,Web前端这个职业也越来越收到企业的重视...
成都web前端培训班哪家比较好...   随着全球数字化的发展,越来越多的企业均布局互联网发展,随之而来的整个人才市场对于IT人才的需求每...
0基础学web前端有多难?前端... 如果问IT编程大佬前端难不难,百分之百会告诉你很简单,但如果问门外汉,那么答案一定是难。那么针对0基...
web前端开发框架有哪些?前端... web前端开发框架有哪些?前端开发日常工作中经常会用到框架,今天我们来看看前端开发收藏的一些前端框架...
30岁学web前端晚不晚?学习... 很多想转行的人已经不年轻了,比如三十岁左右的人,这个年龄段想转行其实不太容易,特别是IT行业,如果想...
web前端框架哪个好?前端三大... Web前端框架在工作场所是必要的。强大的框架可以大大提高技术人员的工作效率。Web前端框架哪个好?前...