前端面试笔试题学习--后延/以后学习01
2018-02-09 17:37
585 查看
1、 实现以下方法(与标准一致)
Element.prototype .getElementsByclassname
element.prototype.getElementsByClassName = function (searchClass, node,tag) {
if(document.getElementsByClassName){
var nodes = (node || document).getElementsByClassName(searchClass),result = [];
for(var i=0 ;node = nodes[i++];){
if(tag !== "*" && node.tagName === tag.toUpperCase()){
result.push(node)
}
}
return result
}else{
node = node || document;
tag = tag || "*";
var classes = searchClass.split(" "),
elements=(tag==="*"&&node.all)?node.all:node.getElementsByTagName(tag),
patterns = [],
current,
match;
var i = classes.length;
while(--i >= 0){
patterns.push(new RegExp("(^|\\s)" + classes[i] + "(\\s|$)"));
}
var j = elements.length;
while(--j >= 0){
current = elements[j];
match = false;
for(var k=0, kl=patterns.length; k<kl; k++){
match = patterns[k].test(current.className);
if (!match) break;
}
if (match) result.push(current);
}
return result;
}
}
Function.Prototype.bind
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
throw new TypeError("bind function error");
}
var aArgs = Array.prototype.slice.call(arguments,1),
fToBind = this,
fBound = function () {
return fToBind.apply(oThis || window,aArgs.concat(Array.prototype.slice.call(arguments)));};
return fBound;
};
Element.prototype .getElementsByclassname
element.prototype.getElementsByClassName = function (searchClass, node,tag) {
if(document.getElementsByClassName){
var nodes = (node || document).getElementsByClassName(searchClass),result = [];
for(var i=0 ;node = nodes[i++];){
if(tag !== "*" && node.tagName === tag.toUpperCase()){
result.push(node)
}
}
return result
}else{
node = node || document;
tag = tag || "*";
var classes = searchClass.split(" "),
elements=(tag==="*"&&node.all)?node.all:node.getElementsByTagName(tag),
patterns = [],
current,
match;
var i = classes.length;
while(--i >= 0){
patterns.push(new RegExp("(^|\\s)" + classes[i] + "(\\s|$)"));
}
var j = elements.length;
while(--j >= 0){
current = elements[j];
match = false;
for(var k=0, kl=patterns.length; k<kl; k++){
match = patterns[k].test(current.className);
if (!match) break;
}
if (match) result.push(current);
}
return result;
}
}
Function.Prototype.bind
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
throw new TypeError("bind function error");
}
var aArgs = Array.prototype.slice.call(arguments,1),
fToBind = this,
fBound = function () {
return fToBind.apply(oThis || window,aArgs.concat(Array.prototype.slice.call(arguments)));};
return fBound;
};
相关文章推荐
- 前端开发面试笔试学习--html/css-01
- 前端开发面试笔试学习--算法01--数组求和/数组去重
- 前端开发面试笔试学习--JavaScript01
- 关于前端学习和笔试面试的总结
- Web前端开发笔试&面试_01(mi:)
- 关于前端学习和笔试面试的总结
- 关于前端学习和笔试面试的总结
- 前端开发,面试笔试题学习---通信/原理02
- 关于前端学习和笔试面试的总结
- 前端开发面试笔试题学习--额额
- 关于前端学习和笔试面试的总结
- 面试笔试整理3:深度学习机器学习面试问题准备(必会)
- 前端面试笔试布局题集合
- 前端学习_Series1_01.HTML5新增标签
- 计算机经典书籍电子书合集(适合计算机学生学习以及程序员笔试、面试)
- 前端学习之路css3(01)-基础知识
- Web前端开发笔试&面试_04
- 笔者总结的一些前端面试笔试试题(仅供参考,如果错误,望指教)--2
- 前端学习-HTML-01
- 阿里2017秋招前端面试笔试题