The new
operator takes a function F
and arguments: new F(arguments...)
. It does three easy steps:
-
Create the instance of the class. It is an empty object with its
__proto__
property set to F.prototype
.
-
Initialize the instance. The function
F
is called with the arguments passed and this
set to be the instance.
-
Return the instance
Now that we understand what the new
operator does, we can implement it in Javascript.
function New (f) {
/*1*/ var n = { '__proto__': f.prototype };
return function () {
/*2*/ f.apply(n, arguments);
/*3*/ return n;
};
}
And just a small test to see that it works.
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype = {
print: function () { console.log(this.x, this.y); }
};
var p1 = new Point(10, 20);
p1.print(); // 10 20
console.log(p1 instanceof Point); // true
var p2 = New (Point)(10, 20);
p2.print(); // 10 20
console.log(p2 instanceof Point); // true
分享到:
相关推荐
Chapter 2, Understanding How Ethereum Works, explains how Ethereum works. Chapter 3, Writing Smart Contracts, shows how to write smart contracts and use geth's interactive console to deploy and ...
At its core Webpack allows us to use javascript modules within our browser by taking multiple files and assets and combining them into one big file as shown below in this image from the new docs for ...
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW); const a_color = gl.getAttribLocation(program, 'a_color'); gl.enableVertexAttribArray(a_color); gl.vertexAttribPointer(a_...
Learn how JavaScript works and why the language is so important in web design Create and optimize web images so they’ll download as quickly as possible NEW! Use CSS Flexbox and Grid for sophisticated...
return new Promise(function(resolve) { setTimeout(function() { resolve('Details for item ' + id); }, 1000); }); } ``` 在这个例子中,JavaScript监听了列表的点击事件,当点击的是列表项时,它获取数据...
Every chapter is packed with example code that works in any JavaScript environment so you'll be able to see new features in action. You'll learn:, * How ECMAScript 6 class syntax relates to more ...
Chapter 2, Understanding How Ethereum Works, explains how Ethereum works. Chapter 3, Writing Smart Contracts, shows how to write smart contracts and use geth's interactive console to deploy and ...
This book shows how to take advantage of the all new features introduced in Bootstrap Learn responsive web design and discover how to build mobile-ready websites with ease Find out how to extend the ...
A handy guide to understanding Microdata, the new JavaScript APIs, and the new form elements in HTML5 and CSS3 along with transition, transformation, and animation using lucid code samples Overview ...
This book shows how to take advantage of the all new features introduced in Bootstrap Learn responsive web design and discover how to build mobile-ready websites with ease Find out how to extend the ...
Starting from getting persistent connections with the server, you will learn the basics of connecting a client to the server and how the messaging works. This will be followed by setting up a hub on ...
•See how JavaScript Object Notation (JSON) works with MySQL •Use JSON as string data and JSON as a data type •Find the path, load data, and handle searches with REGEX •Work with JSON and non-...
how Highcharts/PhantomJS works, and demonstrates the usage and reviews the latest exciting development, Highcharts Cloud—an online chart service without any prior Highcharts or JavaScript experience...
The MVC project chapters dumped the old JavaScript-like event handlers for the sleek new streams interface, and the chapter on testing reflects the extensive changes in testing now available in Dart....
It explains how each type of extension works and describes the application programming interface (API) functions that Dreamweaver calls in order to implement its various objects, menus, floating ...