Javascript 面试题随笔
2011-03-30 14:12
309 查看
var Fundamental = {count:1};
function Test(){}
Test.prototype = Fundamental;
Test.prototype.increase = function(){this.count++;};
var test = new Test();
console.log(test.count);
var test2 = new Test();
console.log(test2.count);
test.increase();
//test.count和test2.count的值各是多少
前天去面试遇到的一道题,面试的问题大概是当test.increase被调用时,test和test2的count值分别是多少
首先,回答这道题有可能把这种情况与另一种类似的情况相混淆:
假如把代码改成:
function FundamentalModified(){
var count = 1;
this.increase = function(){
count++;
}
this.show = function(){
return count;
}
}
function TestModified(){}
TestModified.prototype = new FundamentalModified();
var test3 = new TestModified();
var test4 = new TestModified();
test3.increase(); //test3.show()和test4.show()各是多少
假如问题改成这样,那就简单的多了。但是两个问题并不会得到相同的结果。
==========================================分割一下
回到面试题中,其实面试题的答案是2和1。原因呢:test.count是test的属性,而且test2.count其实是test2.__proto__的属性:
当test.increase()被调用时,JS执行了this.count++ ==> 返回this.count; this.count = this.count + 1;
this.count = this.count + 1;
这句在看似简单的语句其实有着不同寻常的意味~~
这句话的意思其实是,给实例新建一个属性,这个属性被赋予this.count + 1的值。
而this.count 其实是在原型链中的count,也就是这个this.count++其实在第一次执行的时候表现为:
this.count = Test.Prototype.count + 1;
可以用hasOwnProperty来验证一下:
当var test = new Test()时。test.hasOwnProperty("count") === false
test.increase()后。 test.hasOwnProperty("count") === true
总的来说,JS还是一个很好玩的语言。
function Test(){}
Test.prototype = Fundamental;
Test.prototype.increase = function(){this.count++;};
var test = new Test();
console.log(test.count);
var test2 = new Test();
console.log(test2.count);
test.increase();
//test.count和test2.count的值各是多少
前天去面试遇到的一道题,面试的问题大概是当test.increase被调用时,test和test2的count值分别是多少
首先,回答这道题有可能把这种情况与另一种类似的情况相混淆:
假如把代码改成:
function FundamentalModified(){
var count = 1;
this.increase = function(){
count++;
}
this.show = function(){
return count;
}
}
function TestModified(){}
TestModified.prototype = new FundamentalModified();
var test3 = new TestModified();
var test4 = new TestModified();
test3.increase(); //test3.show()和test4.show()各是多少
假如问题改成这样,那就简单的多了。但是两个问题并不会得到相同的结果。
==========================================分割一下
回到面试题中,其实面试题的答案是2和1。原因呢:test.count是test的属性,而且test2.count其实是test2.__proto__的属性:
当test.increase()被调用时,JS执行了this.count++ ==> 返回this.count; this.count = this.count + 1;
this.count = this.count + 1;
这句在看似简单的语句其实有着不同寻常的意味~~
这句话的意思其实是,给实例新建一个属性,这个属性被赋予this.count + 1的值。
而this.count 其实是在原型链中的count,也就是这个this.count++其实在第一次执行的时候表现为:
this.count = Test.Prototype.count + 1;
可以用hasOwnProperty来验证一下:
当var test = new Test()时。test.hasOwnProperty("count") === false
test.increase()后。 test.hasOwnProperty("count") === true
总的来说,JS还是一个很好玩的语言。
相关文章推荐
- Javascript 面试题随笔
- Javascript 面试题随笔之Function.call.call
- Javascript 面试题随笔
- .net面试题(.Net+Html+Javascript)
- 你应该知道的25道Javascript面试题
- 史上最全Javascript 面试题答案
- 随笔:弱类型语言javascript中的一些小坑
- 前端常见算法面试题之 - 从尾到头打印链表[JavaScript解法]
- javascript 面试题
- 收集Web前端 面试题 Javascript部分
- JavaScript 随笔
- 随笔——面试题的道与术
- 极易被忽视的javascript面试题七问七答
- 经典的javascript面试题
- 【 随笔 】 JavaScript 图形库的流行度调查
- 你必须知道的25道JavaScript核心面试题
- JavaScript的基础知识点(面试题)
- javascript 面试题收集
- 你有必要知道的25个JavaScript面试题
- 面试题之请写出用于校验 HTML 文本框中输入的内容全部为数字 的 javascript 代码