


A detailed explanation of why not to use the new keyword in JavaScript
The new keyword in JavaScript can implement instantiation and inheritance, but I personally think that using the new keyword is not the best practice, and there can be a more friendly implementation. This article will introduce what are the problems with using the new keyword, and then introduce how to encapsulate a series of Object-oriented operations associated with new in order to provide a faster and easier-to-understand implementation.
Traditional instantiation and inheritance
Suppose we have two classes, <a href="http://www.php.cn/wiki/164.html" target="_blank">Class</a>:function Class() {}
and SubClass :function SubClass(){}
, SubClass needs to inherit from Class. The traditional method is generally organized and implemented according to the following steps:
- ##In the SubClass
- your own methods and properties
must also be placed in your own prototype attribute
The prototype(proto) attribute of the prototype object of SubClass must point to the prototype of Class - In this way, due to the characteristics of the prototype chain, the instance of SubClass can be traced back to Class Method to achieve inheritance:
new SubClass() Object.create(Class.prototype) | | V V SubClass.prototype ---> { } { }.proto ---> Class.prototype
Give a specific example: In the following code, we do the following things:
- Define a parent class called Human
- Define a subclass named Man that inherits from Human
- The subclass inherits all attributes of the parent class and calls the parent class’s
- Constructor
// 构造函数/基类 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();
Copy after loginDEMO
Through the above code, we can summarize several traditional instantiation and inheritance Features:
- The "class" in the traditional method must be a constructor.
- Attributes and methods are bound to prototype attributes, and inheritance is implemented with the help of prototype characteristics.
- Instantiate an object through the new keyword.
- Why am I so sure that the Object.create method is consistent with Douglas's object method? Because on MDN, the object method is a Polyfill solution for Object.create:
In "Javascript: The Good Parts", Douglas believes that it should be avoided Use the new keyword:
If you forget toincludethe 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)
The general idea is that if you forget the new keyword when you should use new, it will cause some problems.
Of course, any keyword you forget to use will cause a series of problems. Taking a step back, this problem is completely avoidable:
function foo() { // 如果忘了使用关键字,这一步骤会悄悄帮你修复这个问题 if ( !(this instanceof foo) ) return new foo(); // 构造函数的逻辑继续…… }
or the more general
throw an exceptionfunction foo()
{
if ( !(this instanceof arguments.callee) )
throw new Error("Constructor called as a function");
}
// 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 ); }; }
In my opinion, the key reason why the new keyword is not a good practice is :
…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.Douglas described the problem as: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.
Simply put, JavaScript is a prototypical language. When it was first created, it was to cater to the needs of the market and make people feel that it is the same as Java. Similarly, the new keyword was introduced. Javascript is supposed to implement instantiation and inheritance through its Prototypical features, but the new keyword makes it nondescript.
把传统方法加以改造
既然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关键字的基础上继续实现多继承?希望本文的内容在这里只是抛砖引玉,能够开拓大家的思路。
The above is the detailed content of A detailed explanation of why not to use the new keyword in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
