美文网首页
javascript面试题

javascript面试题

作者: yahveyeye | 来源:发表于2017-02-18 09:35 被阅读0次

参考: https://cnodejs.org/topic/5884574e250bf4e2390e9e99
1.写出运行结果

var length = 10;
function fn() {
console.log(this.length)
};
var obj = {
length: 5,
method: function (fn) {
fn();
arguments0;
fn.call(obj, 12);
}
};
obj.method(fn, 1);

答案是:

10 2 5

2 写出在浏览器的运行结果

function Foo() {
getName = function () {
console.log('1');
};
return this;
}
Foo.getName = function () {
console.log('2');
};
Foo.prototype.getName = function () {
console.log('3');
};
var getName = function () {
console.log('4');
};
function getName() {
console.log(5);
}
Foo.getName();
getName();
Foo().getName();
getName();
new Foo.getName();
new Foo().getName();
new new Foo().getName();

答案如下:

2 4 1 1 2 3 3

3 完成plus函数,通过全部的测试用例。

'use strict';
var assert = require('assert')
var plus = require('../lib/assign-4')
describe('闭包应用',function(){
it('plus(0) === 0',function(){
assert.equal(0,plus(0))
})
it('plus(1)(1)(2)(3)(5) === 12',function(){
assert.equal(12,plus(1)(1)(2)(3)(5))
})
it('plus(1)(4)(2)(3) === 10',function(){
assert.equal(10,plus(1)(4)(2)(3))
})
it('方法引用',function(){
var plus2 = plus(1)(1)
assert.equal(12,plus2(1)(4)(2)(3))
})
})

答案如下:

function plus (n) {
var p = function (m) {
return plus(n + m);
}

p.valueOf= function () {
return n;
}

return p;
}

相关文章

网友评论

      本文标题:javascript面试题

      本文链接:https://www.haomeiwen.com/subject/eovqwttx.html