js相关面试题积累。。
1.写出window.foo 的值:
(window.foo || (window.foo = 'bar'));
解:(需注意:先读()内的值。。)
(window.foo || (window.foo = 'bar'));
console.log(window.foo);
//输出 :
// bar
2.使用原生js, addEventListener,给每个li元素绑定一个click事件,并输出他们的顺序
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style: none;
background: green;
height: 40px;
}
li:nth-child(2n) {
background: red
}
</style>
<ul>
<li>a</li>
<li>a</li>
<li>a</li>
<li>a</li>
</ul>
<script>
function test() {
var liCollection = document.getElementsByTagName('li');
for (var i = 0; i < liCollection.length; i++) {
(function (i) {
liCollection[i].addEventListener('click', function () {
console.log(i);
}, false);
}(i));
}
}
test();
</script>
3.
//1.
function retBytesLen(target) {
var count = 0;
for (var i = 0; i < target.length; i++) {
if (target.charCodeAt(i) <= 255) {
count++;
} else if (target.charCodeAt(i) > 255) {
count += 2;
}
}
console.log(count);
}
retBytesLen('abcde');
//2.
function retBytesLen(target) {
var count = target.length;
for (var i = 0; i < target.length; i++) {
if (target.charCodeAt(i) > 255) {
count++;
}
}
console.log(count);
}
retBytesLen('abcde加加');
//3.
function retBytesLen(target) {
var count, len;
count = len = target.length;
for (var i = 0; i < len; i++) {
if (target.charCodeAt(i) > 255) {
count++;
}
}
console.log(count);
}
retBytesLen('abcde加加');
4.写出下程序的执行结果:
var f = (1,2); 返回 2;返回逗号后面的那位;
// f = (a,b) == f = b
var f = (
function f() {
return "1";
},
function g() {
return 2;
}
)();
console.log(typeof f);
// number
5.写出下程序的执行结果:
//function f() {} 放在()中是表达式 不再是个函数。。
//未声明变量放在 typeof 中不会报错 返回 undefined
var x = 1;
if (function f() {}) {
x += typeof f;
}
console.log(x);
// 1undefined
6:
var str = "abc";
str += 1; // abc1
var test = typeof (str); //String
if (test.length == 6) {
test.sign = "typeof返回结果可能是String";
// new String(test).sign = "..."; delete;
}
console.log(test.sign);
//undefined
7:
function foo() {
bar.apply(null, arguments);
}
function bar() {
console.log(arguments);
}
foo(1, 2, 3, 4, 5);
//Arguments(5) [1, 2, 3, 4, 5]
8:typeof能返回结果的有:
string、number、boolean、undefined、object、function
9:
function b(x, y, a) {
arguments[2] = 10;
alert(a);
}
b(1, 2, 3);
a = 10;
alert(arguments[2]);
// 10
// arguments is not defined
10:isNaN() 函数用于检查其参数是否是非数字值
undefined == null true
undefined === null false
isNaN(‘100’) false
parenInt(‘1a’) == 1 true
{} == {} false //对象是代表两个房间所以不相等;
11.
function print() {
console.log(foo);
var foo = 2;
console.log(foo);
console.log(hello);
}
print();
// undefined
// 2
// hello is not defined
12.
function print() {
var test;
test();
function test() {
console.log(1);
}
}
print();
// 1
13:
function print() {
var x = 1;
if (x == '1') console.log('one!');
if (x === '1') console.log('two!');
}
print();
// one!
14:
function print() {
var marty = {
name: 'marty',
printName: function () {
console.log(this.name);
}
}
var test1 = {
name: 'test1'
};
var test2 = {
name: 'test2'
};
var test3 = {
name: 'test3'
};
test3.printName = marty.printName; // func
var printName2 = marty.printName.bind({
name: 123
}); //123
marty.printName.call(test1); // test1
marty.printName.apply(test2); //test2
marty.printName(); //marty
printName2(); //123
test3.printName(); //test3
}
print();
// test1
// test2
// marty
// 123
// test3
13:
var bar = {
a: '002'
};
function print() {
bar.a = 'a';
Object.prototype.b = 'b';
return function inner() {
console.log(bar.a);
console.log(bar.b);
}
}
print()();
// a
// b
14:
15:
持续更新ing。。。