发布于 2016-09-26 05:34:53 | 191 次阅读 | 评论: 0 | 来源: 网友投递
这里有新鲜出炉的Javascript教程,程序狗速度看过来!
JavaScript客户端脚本语言
Javascript 是一种由Netscape的LiveScript发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
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的值各是多少
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()各是多少
当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还是一个很好玩的语言。