前端笔试题 JS部分
题目 http://www.itmian4.com/forum.php?mod=viewthread&tid=4540
http://www.itmian4.com/forum.php?mod=viewthread&tid=4540 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var div=document.createElement('div');
var o={a:1,b:{c:1}};
var obj = {
name: "Simon",
age: "20",
clothing: {
style: "simple",
isDouche: false
}
}
var arr=[1,2,3];
window.onload=function(){
//当输入框获取焦点时 删除原有字符 离开输入框且未输入字符时重新显示原有字符
//checkInput();
//当点击链接/按钮时 显示这是当前第几个链接/按钮
//countLink();
//countBtn();
//countBtn2();
//获取全部父节点名字
//getAllParents(document.getElementById('getParent'));
// 给定一个对象var o={a:1,b:{c:1}}; 取到全部属性的名字 如abc
//recursion(o);
//recursion(obj);
//比较除了第一个字符之外剩余字串 并按顺序排列
//asort();
//为数组添加Shuffle()方法
// addShuffle();
// testShuffle();
// 将两个数组合并在一起
//mergeArray();
// 判断是不是数组
//isArray(arr);
//alertMath();
//数组去重
//deleteDupligate();
//addUniqueForArray();
// 判断是否是String类型 'xxx' new String('xxx') 都要能够正确判断
//testString();
//显示标签名称
//alertName();
}
// all props
function allProp(){
for(prop in div.style){
str+=(prop+=' ');
}
document.getElementById('msg').innerHTML=str;
}
function checkProp(prop){
if(prop in div.style){
return true;
}else{
return false;
}
}
function checkInput(){
var input=document.getElementsByName('input1')[0];
//下面这种定义事件的方式看起来简单但是不好
//看起来定义了为blur事件定义了两个函数 //实际上后面一个函数覆盖了前面一个 最终只会弹出b2
// input.onblur=function(){
// alert('blur');
// }
// input.onblur=function(){
// alert('b2');
// }
//所以应该使用下面的方式
input.addEventListener('focus',function(){
if(this.value=='default'){
this.value='';
}
},false);
input.addEventListener('blur',function(){
if(this.value==''){
this.value='default';
}
}); } function countLink(){
var sum=document.getElementsByTagName('a').length;
console.log(sum);
for (var i = 0; i < sum; i++) {
document.getElementsByTagName('a')[i].onclick=function(x){
return function(){
alert(x);
}
}(i); //使用闭包Closure的方式向匿名函数传参
}
}
//关于事件传参使用Closure http://stackoverflow.com/questions/10856517/javascript-issue-with-scope-and-passing-parameters-to-dynamically-created-even function countBtn(){
var all=document.getElementsByTagName('button');
var sum=document.getElementsByTagName('button').length;
for (var i = 0; i < all.length; i++) {
all[i].addEventListener('click',function(x) {
return function(){
alert(x);
}
}(i));
}
}
// 为什么要用return check this
// http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example
function countBtn2(){
var all=document.getElementsByTagName('button');
for (var i = 0; i < all.length; i++) {
all[i].addEventListener('click',closureForCountBtn2(i));
}
}
function closureForCountBtn2(x){
return function(){
alert(x);
}
}
function getAllParents(dom){
if (dom.tagName=='html') {
console.log('html it is the root');
}else{
console.log(dom.tagName);
getAllParents(dom.parentNode)
}
} function recursion(o){
for(var name in o){
if(o.hasOwnProperty(name)){
console.log(name+" "+typeof(o[name])+" "+(o[name] instanceof Object));
if(o[name] instanceof Object){
recursion(o[name]);
}
}
}
} function asort(){
var arr=["abd","cba","ba"];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length-i-1; j++) {
//if(strcmp(arr[j],arr[j+1])==1){
//现在的要求是只比较除了第一个字符之外的剩余字符
if(strcmp(arr[j].substring(1),arr[j+1].substring(1))){
temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
};
};
for(var index in arr){
console.log(arr[index]);
}
} function strcmp(str1,str2){
return ( str1 < str2 ) ? -1 : ( str1 > str2 ? 1 : 0 );
} function addShuffle(){
Array.prototype.shuffle=function(){
var len=this.length;
for(var i=0,j=0;i<len;i++){
j=(function(x){
return Math.floor(Math.random()*x);
}(len));
temp=this[i];
this[i]=this[j];
this[j]=temp; }
return this;
}
}
function getRandom(len){
return Math.floor(Math.random()*len);
}
function testShuffle(){
var arr=[1,2,3,4,5,6,7,8,9,10];
arr.shuffle();
console.log(arr);
} function mergeArray(){
var a = [-3,-1,0,1,3,5,7,9];
var b = [-4,-2,0,2,3,4,5,6,7,8];
for (p in b) {
a.push(p);
}
a.sort();
console.log(a);
} function isArray(obj){
//alert(obj instanceof Array);
//alert(obj.constructor==Array); //constructor
console.log('constructor---------------');
console.log(Object.constructor);//function Function() { [native code] }
console.log(typeof Object);//function 可执行对象说一律返回function
console.log(Object instanceof Object);//true
console.log(typeof Object.constructor);//function
console.log(Array.constructor);//function Function() { [native code] }
console.log({}.constructor);//function Object() { [native code] }
console.log([].constructor);//function Array() { [native code] } //constructor ==
var arr=[1,2,3];
console.log(arr.constructor);//function Array() { [native code] }
console.log(arr.constructor==Array);//true //prototype
console.log('---------------');
console.log(Object.prototype);//Object {}
console.log(Object.prototype.constructor);//function Object() { [native code] }
console.log({}.prototype);//undefined //根据犀牛书121页 字面量对象的原型就是Object.prototype
var F=function(){};
console.log(F);
console.log(F.prototype.constructor);//function (){}
console.log(F.prototype);//Object {} //由此可见 函数的prototye属性是一个对象
console.log(typeof F.prototype);
console.log((function(){}).prototype);//Object {}
console.log((function(){}).prototype.constructor);//function (){}
// //console.log(arr.prototype.constructor);//Uncaught TypeError: Cannot read property 'constructor' of undefined
//console.log({}.prototype.constructor);//Uncaught TypeError: Cannot read property 'constructor' of undefined //var o1= Object.create([1,2,3]);
// console.log(o1.prototype);//undefined
console.log('----------------');
function Animal(name){ }
console.log(typeof Animal.prototype);//object
console.log(Animal.prototype);//Animal {}
} function alertMath(){
var a= (Math.PI++);
var b = (Math.PI++);
alert(a);//3.14...
alert(b); } function deleteDupligate(){
var arr=[1,1,2,3,4,4,5,6,7,7,8,9];
for (var i = 1; i < arr.length; i++) {
if(isExists(i,arr)){
mymove(i,arr);
arr.length=arr.length-1;
}
}
console.log(arr);
}
function isExists(i,arr){
flag=false;
for (var j = 0; j < i; j++) {
if(arr[i]==arr[j]){
flag=true;
break;
}
}
return flag;
} function mymove(i,arr){
for(j=i;j<arr.length-1;j++){
arr[j]=arr[j+1];
}
} /*上面去除重复元素的方法太C语言了 */
// 看看这个帖子 http://php.js.cn/blog/array-unique-in-javascript/
//原文中第4中实现似乎是最高效的方式 但是改变了元素顺序 所以还是写第2种好了
function addUniqueForArray(){
//为数组添加function
Array.prototype.unique4=function(){
var uni={};//不重复元素列表 //key-value形式
var re=[];
for (var i = 0; i < this.length; i++) {
if(!uni[this[i]]){
uni[this[i]]=true;//值设置为true
re.push(this[i]);
}
};
return r;
}
}
function classof(obj){
if(obj===null)return null;
if(obj===undefined) return undefined;
// 注意 null undefined在使用==的时候被认为是相同的
//所以 第一个判断上如果使用的是== 是无法判断究竟是null还是undefined
return Object.prototype.toString.call(obj).slice(8,-1);
}
function testString(){
var isString=function(str){
return (typeof str=='string');//这种方式不能检测包装对象
}
console.log(isString('sdcds'));//true
console.log(isString(new String('scsc')));//false var isString2=function(arg){
return ((typeof arg=='string')||(arg.constructor==String));
}
console.log(isString2(new String('')));//true
var isString3=function(arg){
//return ((typeof arg=='string')||(typeof classof(arg)=='string'));
return ((typeof arg=='string')||(classof(arg)=='String'));
}
console.log(isString3(new String('')));//true
} function alertName(){
var all=document.getElementsByTagName('a');
for (var i = 0; i < all.length; i++) {
console.log(all[i].tagName);
};
} // 异步加载script
function loadScript(url, callback) {
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState){//IE
script.onreadystatechange = function(){
if (script.readyState == "loaded"||script.readyState == "complete"){
script.onreadystatechange =null;
callback();
}
};
}else{//Others: Firefox, Safari, Chrome,and Opera script.onload = function()
{
callback();
};
}
script.src = url;
document.body.appendChild(script);
}
</script>
</head> <body>
<div class='root'>
<a href="javascript:void(0);">111</a>
<button>1</button>
<div>
<a href="javascript:void(0);">222</a>
<button>
<span id='getParent'>2</span>
</button>
<div>
<a href="javascript:void(0);">323</a>
<button>3</button>
</div>
</div> </div>
</body>
</html>
前端笔试题 JS部分的更多相关文章
- 也许你需要点实用的-Web前端笔试题
之前发的一篇博客里没有附上答案,现在有空整理了下发出来,希望能帮助到正在找工作的你,还是那句话:技术只有自己真正理解了才是自己的东西,共勉. Web前端笔试题 Html+css 1.对WEB标准以及w ...
- 前端笔试题2 JS部分
笔试题2 var EventME = {}; EventME.addHandle = function(ele, eventName, handleFunction) { if (ele.addEve ...
- 2015年百度实习生前端笔试题上海卷a
1.写出javascript运行结果:alert(‘5’+5); 结果:’55’ 2.写出javascript运行结果:for(var i=0; i<10; i++){} alert(i); 结 ...
- 2016届百度实习生前端笔试题上海卷a
1.写出javascript运行结果:alert(‘5’+5); 结果:alert()函数中不能进行算术运算或字符串拼接,故不会弹出对话框. 2.写出javascript运行结果:for(var ...
- 2015腾讯web前端笔试题
1 请实现,鼠标点击页面中的任意标签,alert该标签的名称.(注意兼容性) 2 请指出一下代码的性能问题,并经行优化. var info="腾讯拍拍网(www.paipai.com)是 ...
- web前端笔试题
1, 判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母.数字.下划线,总长度为5-20 var reg = /^[a-zA-Z][a-zA-Z_0-9]{4,19}$/; reg.test ...
- 腾讯web前端笔试题及个人答案
每道题都有答案,大多数答案亲测正确. 简答题 1.js中“5”+4=? 答案:54 2.js中void(0)=? 答案:undefined 3.js中NaN*4=? 答案:NaN 4.js中null* ...
- web前端笔试题总结
em和rem的区别: 浏览器的默认字体高度是16px,1em=16px:大小可以自己设置调整,并且默认集成父级容器中文本的大小. rem是CSS3中新增的属性,默认情况下是文本尺寸的大小,不同的是它集 ...
- 腾讯2013笔试题—web前端笔试题 (老题练手)
问题描述(web前端开发附加题1): 编写一个javascript的函数把url解析为与页面的javascript.location对象相似的实体对象,如:url :'http://www.qq.co ...
随机推荐
- Swift学习之十四:闭包(Closures)
* 闭包(Closures) * 闭包是自包含的功能代码块,可以在代码中使用或者用来作为参数传值. * 在Swift中的闭包与C.OC中的blocks和其它编程语言(如Python)中的lambdas ...
- 【原创】重绘winform的GroupBox
功能:重绘winform的GroupBox,以便调整边框颜色和边框宽度 using System; using System.Collections.Generic; using System.Com ...
- MS SQL:ID自增列从1开始重新排序
数据库中把ID自增长重置成1: 一般做法:(太麻烦) 复制表数据->删除原表.新建一张表->粘贴: 新方法: 数据库中:新建查询->复制.粘贴一下代码->修改表名,执行即可(先 ...
- jquery + ajax调用后台方法
前台js: var parameter = ""; $.ajax({ type: "POST", //提交方式 url: "Default.aspx/ ...
- WPF利用依赖属性和命令编写自定义控件
以实例讲解(大部分讲解在代码中) 1,新建一个WPF项目,添加一个用户控件之后在用户控件里面添加几个控件用作测试, <UserControl x:Class="SelfControlD ...
- MFC的资源切换AFX_MANAGE_STATE(AfxGetStaticModuleState()
转载自:http://blog.chinaunix.net/uid-20532101-id-1931929.html 以前写MFC的DLL的时候,总会在自动生成的代码框架里看到提示,需要在每一个输出的 ...
- ByteBuffer常用方法详解
原文 http://blog.csdn.net/u012345283/article/details/38357851 缓冲区(Buffer)就是在内存中预留指定大小的存储空间用来对输入/输出(I/ ...
- Struts2拦截器总结
拦截器的本质: 拦截器就是一个类,一个实现了超级接口Interceptor的类.Interceptor接口里定义了三个方法 init(),destory(),intercept().其中inercep ...
- table操作:边框-斑马线-多表头-焦点高亮-自动求和
一.操作table,本例子实现的功能: 1.table等宽边框2.table斑马线3.实现table多表头4.焦点所在行高亮5.自动计算总分 二.效果图 三.代码: <!DOCTYPE html ...
- Listview 多个ViewHolder实现
简单代码示例: package com.edaixi.adapter; import android.content.Context; import android.view.View; import ...