`
Blackbaby
  • 浏览: 183825 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

JavaScript:How new works

阅读更多

The new operator takes a function F and arguments: new F(arguments...). It does three easy steps:

  1. Create the instance of the class. It is an empty object with its __proto__ property set to F.prototype.
  2. Initialize the instance. The function F is called with the arguments passed and this set to be the instance.
  3. 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

 

 

分享到:
评论

相关推荐

    Building Blockchain Projects -Packt Publishing((2017).pdf )

    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 ...

    how-webpack-works.pdf

    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 ...

    WebGL How It Works

    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_...

    Learning Web Design: A Beginner’s Guide to HTML... ..., 5th Edition

    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...

    DOM-and-How-Javascript-works

    return new Promise(function(resolve) { setTimeout(function() { resolve('Details for item ' + id); }, 1000); }); } ``` 在这个例子中,JavaScript监听了列表的点击事件,当点击的是列表项时,它获取数据...

    Understanding ECMAScript 6

    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 ...

    Building Blockchain Projects_Develop Real-Packt Publishing(2017)

    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 ...

    Learning.Bootstrap.4.2nd.Edition.1785881000

    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 ...

    HTML5 and CSS3 Transition, Transformation, and Animation

    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 ...

    Learning Bootstrap 4 - Second Edition

    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 ...

    SignalR – Real-time Application Development, 2nd (pdf英文原版第二版)

    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 ...

    MySQL and JSON A Practical Programming Guide 2018

    •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-...

    Learning Highcharts 4(PACKT,2015)

    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...

    Dart 1 for Everyone(Pragmatic,2014)

    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....

    Macromedia Dreamweaver 8

    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 ...

Global site tag (gtag.js) - Google Analytics