JavaScript中的new关键字可以实现实例化和继承的工作,但个人认为使用new关键字并非是最佳的实践,还可以有更友好一些的实现。本文将介绍使用new关键字有什么问题,然后介绍如何对与new相关联的一系列面向对象操作进行封装,以便提供更快捷的、更易让人理解的实现方式。
传统的实例化与继承
假设我们有两个类,<a href="http://www.php.cn/wiki/164.html" target="_blank">Class</a>:function Class() {}
和SubClass:function SubClass(){}
,SubClass需要继承自Class。传统方法一般是按如下步骤来组织和实现的:
Class中被继承的属性和方法必须放在Class的prototype属性中
SubClass中自己的方法和属性也必须放在自己prototype属性中
SubClass的prototype对象的prototype(proto)属性必须指向的Class的prototype
这样一来,由于prototype链的特性,SubClass的实例便能追溯到Class的方法,从而实现继承:
new SubClass() Object.create(Class.prototype) | | V V SubClass.prototype ---> { } { }.proto ---> Class.prototype
举一个具体的例子:下面的代码中,我们做了以下几件事:
定义一个父类叫做Human
定义一个名为Man的子类继承自Human
子类继承父类的一切属性,并调用父类的构造函数,实例化这个子类
// 构造函数/基类 function Human(name) { this.name = name; } /* 基类的方法保存在构造函数的prototype属性中 便于子类的继承 */ Human.prototype.say = function () { console.log("say"); } /* 道格拉斯的object方法(等同于object.create方法) */ function object(o) { var F = function () {}; F.prototype = o; return new F(); } // 子类构造函数 function Man(name, age) { // 调用父类的构造函数 Human.call(this, name); // 自己的属性age this.age = age; } // 继承父类的方法 Man.prototype = object(Human.prototype); Man.prototype.constructor = Man; // 实例化子类 var man = new Man("Lee", 22); console.log(man); // 调用父类的say方法: man.say();
DEMO
通过上面的代码可以总结出传统的实例化与继承的几个特点:
传统方法中的“类”一定是一个构造函数。
属性和方法绑定在prototype属性上,并借助prototype的特性实现继承。
通过new关键字来实例化一个对象。
为什么我会十分的肯定Object.create方法与道格拉斯的object方法是一致呢?因为在MDN上,object方法就是作为Object.create的一个Polyfill方案:
Object.create
Douglas Crockford’s object method
new关键字的不足之处
在《Javascript语言精粹》(Javascript: The Good Parts)中,道格拉斯认为应该避免使用new关键字:
If you forget to include the new prefix when calling a constructor function, then this will not be bound to the new object. Sadly, this will be bound to the global object, so instead of augmenting your new object, you will be clobbering global variables. That is really bad. There is no compile warning, and there is no runtime warning. (page 49)
大意是说在应该使用new的时候如果忘了new关键字,会引发一些问题。
当然了,你遗忘使用任何关键字都会引起一系列的问题。再退一步说,这个问题是完全可以避免的:
function foo() { // 如果忘了使用关键字,这一步骤会悄悄帮你修复这个问题 if ( !(this instanceof foo) ) return new foo(); // 构造函数的逻辑继续…… }
或者更通用的抛出异常即可
function foo() { if ( !(this instanceof arguments.callee) ) throw new Error("Constructor called as a function"); }
又或者按照John Resig的方案,准备一个makeClass工厂函数,把大部分的初始化功能放在一个init方法中,而非构造函数自己中:
// makeClass - By John Resig (MIT Licensed) function makeClass(){ return function(args){ if ( this instanceof arguments.callee ) { if ( typeof this.init == "function" ) this.init.apply( this, args.callee ? args : arguments ); } else return new arguments.callee( arguments ); }; }
在我看来,new关键字不是一个好的实践的关键原因是:
…new is a remnant of the days where JavaScript accepted a Java like syntax for gaining “popularity”. And we were pushing it as a little brother to Java, as a complementary language like Visual Basic was to C++ in Microsoft’s language families at the time.
道格拉斯将这个问题描述为:
This indirection was intended to make the language seem more familiar to classically trained programmers, but failed to do that, as we can see from the very low opinion Java programmers have of JavaScript. JavaScript’s constructor pattern did not appeal to the classical crowd. It also obscured JavaScript’s true prototypal nature. As a result, there are very few programmers who know how to use the language effectively.
简单来说,JavaScript是一种prototypical类型语言,在创建之初,是为了迎合市场的需要,让人们觉得它和Java是类似的,才引入了new关键字。Javascript本应通过它的Prototypical特性来实现实例化和继承,但new关键字让它变得不伦不类。
把传统方法加以改造
既然new关键字不够友好,那么我们有两个办法可以解决这个问题:一是完全抛弃new关键字,二是把含有new关键字的操作封装起来,只向外提供友好的接口。下面将介绍第二种方法的实现思路,把传统方法加以改造。
我们开始构造一个最原始的基类Class
(类似于JavaScript中的Object类),并且只向外提供两个接口:
Class.extend 用于拓展子类
Class.create 用于创建实例
// 基类 function Class() {} // 将extend和create置于prototype对象中,以便子类继承 Class.prototype.extend = function () {}; Class.prototype.create = function () {}; // 为了能在基类上直接以.extend的方式进行调用 Class.extend = function (props) { return this.prototype.extend.call(this, props); }
extend和create的具体实现:
Class.prototype.create = function (props) { /* create实际上是对new的封装; create返回的实例实际上就是new构造出的实例; this即指向调用当前create的构造函数; */ var instance = new this(); /* 绑定该实例的属性 */ for (var name in props) { instance[name] = props[name]; } return instance; } Class.prototype.extend = function (props) { /* 派生出来的新的子类 */ var SubClass = function () {}; /* 继承父类的属性和方法, 当然前提是父类的属性都放在prototype中 而非上面create方法的“实例属性”中 */ SubClass.prototype = Object.create(this.prototype); // 并且添加自己的方法和属性 for (var name in props) { SubClass.prototype[name] = props[name]; } SubClass.prototype.constructor = SubClass; /* 介于需要以.extend的方式和.create的方式调用: */ SubClass.extend = SubClass.prototype.extend; SubClass.create = SubClass.prototype.create; return SubClass; }
仍然以Human和Man类举例使用说明:
var Human = Class.extend({ say: function () { console.log("Hello"); } }); var human = Human.create(); console.log(human) human.say(); var Man = Human.extend({ walk: function () { console.log("walk"); } }); var man = Man.create({ name: "Lee", age: 22 }); console.log(man); // 调用父类方法 man.say(); man.walk();
DEMO
至此,基本框架已经搭建起来,接下来继续补充功能。
我们希望把构造函数独立出来,并且统一命名为init。就好像
Backbone.js
中每一个view都有一个initialize
方法一样。这样能让初始化更灵活和标准化,甚至可以把init构造函数借出去我还想新增一个子类方法调用父类同名方法的机制,比如说在父类和子类的中都定义了一个say方法,那么只要在子类的say中调用
this.callSuper()
就能调用父类的say方法了。例如:
// 基类 var Human = Class.extend({ /* 你需要在定义类时定义构造方法init */ init: function () { this.nature = "Human"; }, say: function () { console.log("I am a human"); } }) var Man = Human.extend({ init: function () { this.sex = "man"; }, say: function () { // 调用同名的父类方法 this.callSuper(); console.log("I am a man"); } });
那么Class.create就不仅仅是new一个构造函数了:
Class.create = Class.prototype.create = function () { /* 注意在这里我们只是实例化一个构造函数 而非最后返回的“实例”, 可以理解这个实例目前只是一个“壳” 需要init函数对这个“壳”填充属性和方法 */ var instance = new this(); /* 如果对init有定义的话 */ if (instance.init) { instance.init.apply(instance, arguments); } return instance; }
实现在子类方法调用父类同名方法的机制,我们可以借用John Resig的方案:
Class.extend = Class.prototype.extend = function (props) { var SubClass = function () {}; var _super = this.prototype; SubClass.prototype = Object.create(this.prototype); for (var name in props) { // 如果父类同名属性也是一个函数 if (typeof props[name] == "function" && typeof _super[name] == "function") { // 重新定义用户的同名函数,把用户的函数包装起来 SubClass.prototype[name] = (function (super_fn, fn) { return function () { // 如果用户有自定义callSuper的话,暂存起来 var tmp = this.callSuper; // callSuper即指向同名父类函数 this.callSuper = super_fn; /* callSuper即存在子类同名函数的上下文中 以this.callSuper()形式调用 */ var ret = fn.apply(this, arguments); this.callSuper = tmp; /* 如果用户没有自定义的callsuper方法,则delete */ if (!this.callSuper) { delete this.callSuper; } return ret; } })(_super[name], props[name]) } else { // 如果是非同名属性或者方法 SubClass.prototype[name] = props[name]; } .. } SubClass.prototype.constructor = SubClass; }
最后给出一个完整版,并且做了一些优化:
function Class() {} Class.extend = function extend(props) { var prototype = new this(); var _super = this.prototype; for (var name in props) { if (typeof props[name] == "function" && typeof _super[name] == "function") { prototype[name] = (function (super_fn, fn) { return function () { var tmp = this.callSuper; this.callSuper = super_fn; var ret = fn.apply(this, arguments); this.callSuper = tmp; if (!this.callSuper) { delete this.callSuper; } return ret; } })(_super[name], props[name]) } else { prototype[name] = props[name]; } } function Class() {} Class.prototype = prototype; Class.prototype.constructor = Class; Class.extend = extend; Class.create = Class.prototype.create = function () { var instance = new this(); if (instance.init) { instance.init.apply(instance, arguments); } return instance; } return Class; }
下面是测试的代码。为了验证上面代码的健壮性,故意实现了三层继承:
var Human = Class.extend({ init: function () { this.nature = "Human"; }, say: function () { console.log("I am a human"); } }) var human = Human.create(); console.log(human); human.say(); var Man = Human.extend({ init: function () { this.callSuper(); this.sex = "man"; }, say: function () { this.callSuper(); console.log("I am a man"); } }); var man = Man.create(); console.log(man); man.say(); var Person = Man.extend({ init: function () { this.callSuper(); this.name = "lee"; }, say: function () { this.callSuper(); console.log("I am Lee"); } }) var person = Person.create(); console.log(person); person.say();
DEMO
是时候彻底抛弃new关键字了
如果不使用new关键字,那么我们需要转投上两节中反复使用的Object.create
来生产新的对象
假设我们有一个矩形对象:
var Rectangle = { area: function () { console.log(this.width * this.height); } };
借助Object.create,我们可以生成一个拥有它所有方法的对象:
var rectangle = Object.create(Rectangle);
生成之后,我们还可以给这个实例赋值长宽,并且取得面积值
var rect = Object.create(Rectangle); rect.width = 5; rect.height = 9; rect.area();
注意这个过程我们没有使用new关键字,但是我们相当于实例化了一个对象(rectangle),给这个对象加上了自己的属性,并且成功调用了类(Rectangle)的方法。
但是我们希望能自动化赋值长宽,没问题,那就定义一个create方法:
var Rectangle = { create: function (width, height) { var self = Object.create(this); self.width = width; self.height = height; return self; }, area: function () { console.log(this.width * this.height); } };
使用方式如下:
var rect = Rectangle.create(5, 9); rect.area();
在纯粹使用Object.create的机制下,我们已经完全抛弃了构造函数这个概念。一切都是对象,一个类也可以是对象,这个类的实例不过是一个它自己的复制品。
下面看看如何实现继承。我们现在需要一个正方形,继承自这个长方形
var Square = Object.create(Rectangle); Square.create = function (side) { return Rectangle.create.call(this, side, side); }
实例化它:
var sq = Square.create(5); sq.area();
这种做法其实和我们第一种最基本的类似
function Man(name, age) { Human.call(this, name); this.age = age; }
上面的方法还是太复杂了,我们希望进一步自动化,于是我们可以写这么一个extend函数
function extend(extension) { var hasOwnProperty = Object.hasOwnProperty; var object = Object.create(this); for (var property in extension) { if (hasOwnProperty.call(extension, property) || typeof object[property] === "undefined") { object[property] = extension[property]; } } return object; } /* 其实上面这个方法可以直接绑定在原生的Object对象上:Object.prototype.extend 但个人不推荐这种做法 */ var Rectangle = { extend: extend, create: function (width, height) { var self = Object.create(this); self.width = width; self.height = height; return self; }, area: function () { console.log(this.width * this.height); } };
这样当我们需要继承时,就可以像前几个方法一样用了
var Square = Rectangle.extend({ // 重写实例化方法 create: function (side) { return Rectangle.create.call(this, side, side); } }) var s = Square.create(5); s.area();
结束语
本文对去new关键字的方法做了一些罗列,但工作还远远没有结束,有非常多的地方值得拓展,比如:如何重新定义instance of
方法,用于判断一个对象是否是一个类的实例?如何在去new关键字的基础上继续实现多继承?希望本文的内容在这里只是抛砖引玉,能够开拓大家的思路。
以上是为什么不在JavaScript中使用new关键字的详解的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

Python和JavaScript在社区、库和资源方面的对比各有优劣。1)Python社区友好,适合初学者,但前端开发资源不如JavaScript丰富。2)Python在数据科学和机器学习库方面强大,JavaScript则在前端开发库和框架上更胜一筹。3)两者的学习资源都丰富,但Python适合从官方文档开始,JavaScript则以MDNWebDocs为佳。选择应基于项目需求和个人兴趣。

从C/C 转向JavaScript需要适应动态类型、垃圾回收和异步编程等特点。1)C/C 是静态类型语言,需手动管理内存,而JavaScript是动态类型,垃圾回收自动处理。2)C/C 需编译成机器码,JavaScript则为解释型语言。3)JavaScript引入闭包、原型链和Promise等概念,增强了灵活性和异步编程能力。

不同JavaScript引擎在解析和执行JavaScript代码时,效果会有所不同,因为每个引擎的实现原理和优化策略各有差异。1.词法分析:将源码转换为词法单元。2.语法分析:生成抽象语法树。3.优化和编译:通过JIT编译器生成机器码。4.执行:运行机器码。V8引擎通过即时编译和隐藏类优化,SpiderMonkey使用类型推断系统,导致在相同代码上的性能表现不同。

JavaScript在现实世界中的应用包括服务器端编程、移动应用开发和物联网控制:1.通过Node.js实现服务器端编程,适用于高并发请求处理。2.通过ReactNative进行移动应用开发,支持跨平台部署。3.通过Johnny-Five库用于物联网设备控制,适用于硬件交互。

我使用您的日常技术工具构建了功能性的多租户SaaS应用程序(一个Edtech应用程序),您可以做同样的事情。 首先,什么是多租户SaaS应用程序? 多租户SaaS应用程序可让您从唱歌中为多个客户提供服务


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器