这里有新鲜出炉的 Javascript 教程,程序狗速度看过来!
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
这篇文章主要介绍了 javascript 中 call 和 apply 的用法示例分析,非常的详细, 需要的朋友可以参考下
call 和 apply 的用法,并利用 call 实现 js 类的继承
- /*
- * 矩形
- */
- function Rectangle(len,width) {
- this.len = len;
- this.width = width;
- }
- /*
- * 乘以
- */
- function multiply(a,b) {
- return a * b;
- }
- // 矩形实例
- var rectangle = new Rectangle(15, 30);
- //求矩形面积
- var proportion = multiply.call(rectangle,rectangle.len, rectangle.width);
- // 等价于call
- //var proportion = multiply.apply(rectangle,[rectangle.len, rectangle.width]);
- document.write("矩形的面积是:"+proportion);
- document.write("<br/>");
- document.write("/***********************分割线********************************/<br/>");
- // 实现继承
- function Persion(name) {
- this.name = name;
- this.sayHello = function () {
- return "hello,"+this.name;
- }
- }
- function Student(name,sex,school) {
- Persion.call(this,name);
- this.sex = sex;
- this.school = school;
- this.mySex = function () {
- return this.sex;
- }
- this.mySchool = function () {
- return this.school;
- }
- }
- var stu = new Student('fengjx','男','广西机电职业技术学院')
- document.write("stu sayHello:"+stu.sayHello());
- document.write("<br/>");
- document.write("stu sex is:"+stu.mySex());
- document.write("<br/>");
- document.write("stu school is :"+stu.mySchool());
- document.write("<br/>");
演示图:
来源: http://www.phperz.com/article/17/0418/271988.html