Notes on JavaScript functions and objects
Notes on JavaScript functions and objects
functions
static (or definition) context
* All functions have a property named prototype.1 The value of prototype is an object2 with a property named constructor.3 The value of constructor is the function itself.4
* All functions have a property called length which specifies the expected number of parameters.5
* The value of constructor property of a function is function Function6 that is because the hidden super instance of a function is the value of the prototype property of function Function.7
execution context
* While a function executes there is a special variable arguments holds the actual arguments that were passed in when the function was invoked.
* While a function executes there is a special variable arguments is of type Arguments which is an array-like object with a property length which gives the number of actual parameters passed in during invocation.
* While a function executes there is a special variable arguments.callee holds a reference to the function object itself. Thus arguments.callee.length gives us the number of expected parameters.
Function function
static (or definition) context
* Function being a function has a property named prototype. The value of prototype is an anonymous function function(){} with a property named constructor. The value of constructor is the function Function.
* The value of constructor property of function Function is function Function. That is because the hidden super instance of a function is the value of the prototype property of function Function.
objects such as {} or new Object()
* The constructor of an object such as {} or new Object{} of course is function Object.8
* All objects inherit properties from a hidden super instance - the prototype object. The prototype object is the same as the value of the prototype property of function which was used to create the object using the new operator. Note: objects do not have a property named prototype.
* The inherited properties behave in a copy-on-set manner.
* All objects inherit a property named constructor from their hidden super instance - the prototype object.9
Object function
static (or definition) context
* Object being a function has a property named prototype. The value of prototype is an anonymous object {} with a property named constructor.10 The value of constructor is the function Object.11
* The value of constructor property of function Object is function Function. That is because the hidden super instance of a function is the value of the prototype property of function Function.
Let us say we have code like this:
function Rectangle(width, height) {
this.width = width;
this.height = height;
}
var twoByFourRectangle = new Rectabgle(2, 4);
+--------------------------------------+
inherits | +---------constructor property ----+ | +----------------------------------+
from | | | | inherits | |
| v | v from v |
function Function --- prototype property---> function(){} <----- function Object --- prototype property---> {constructor: Object}
^ ^
inherits | +---------------------------------------+ |
from | | | | inherits
| v | | from(?)
function Rectangle --- prototype property ----> {constructor: Rectangle}--+
^
inherits |
from |
|
object twoByFourRectangle --- width property ----> 2
+--- height property --> 4
1 alert("prototype" in Rectangle); => 'true'
2 alert(Rectangle.prototype); => '[object Object]'
3 alert("constructor" in Rectangle.prototype); => 'true'
4 alert(Rectangle.prototype.constructor); => '[function Rectangle]'
5 alert(Rectangle.length); => '2'
6 alert(Rectangle.constructor) => 'function Function() {[native code]}'
7 alert(Rectangle.constructor.prototype) => 'function(){}'
8 alert({}.constructor); => 'function Object() {[native code]}'
9 alert("constructor" in {}); => 'true'
10 alert(Object.prototype); => '[object Object]'
11 alert(Object.prototype.constructor); => 'function Object() {[native code]}'
TIP: You can evaluate expressions like the above in the Firefox's Tools:Error Console's Evaluate text field.
Tricky huh? It was for me.
分享到:
相关推荐
在当今信息时代,数据结构和编程技术成为了计算机科学不可或缺的核心内容。通过深入探讨数据结构与编程技术,学生们能够掌握如何高效地组织和处理数据,这对于解决各类计算问题具有至关重要的意义。...
《Lecture Notes on Cryptography》是麻省理工学院(MIT)和加州大学圣地亚哥分校(UCSD)联合编写的密码学课程讲义,由Shafi Goldwasser和Mihir Bellare共同编撰,发布于2001年。这份讲义不仅是一份学术资源,也是密码...
【船级社】 ABS Guidance Notes on Design and Installation of Drag Anchors and Plate Anchors,.pdf
#### 标题解读:《Notes on Programming of C++, by Alexander Stepanov》 此标题表明这是一本由Alexander Stepanov撰写的关于C++编程的笔记。Alexander Stepanov是C++标准模板库(STL)的主要设计者之一,在计算机...
lecture notes for use in the course Functional Programming (CSCI 555) that is taught in the Department of Computer and Information Science at the Uni- versity of Mississippi. The language is Haskell
由麻省理工学院(MIT)的作者讲授的一组283页的夏季密码学课程讲义。
本书是基于leetcode的数据结构和算法学习笔记,支持可以查看git上源码
Lecture Notes on ADVANCED INDUSTRIAL ECO NOMICS 2th Stephen Martin =ADVANCED INDU STRIAL ECONOMICS 2th Stephen Martin Lect ure Notes on ADVANCED INDUSTRIAL ECONOMI CS 2th Stephen Martin Lecture Notes ...
《Lecture Notes on Cryptography》是由麻省理工学院(MIT)的Shafi Goldwasser教授与加州大学圣地亚哥分校(UCSD)的Mihir Bellare教授共同编写的讲义集。这是一套关于密码学的讲义,主要面向在1996年至2001年夏...
This report is a description of Mie-Scattering and Mie-Absorption programs written ...description of computational problems in the Mie calculations, see the notes on p. 126-129 and in Appendix A of BH.
会议的全称是“The 11th International Symposium on Formal Methods for Components and Objects”,其中“ABS LANGUAGE”暗示了学术讨论中的一个具体话题或技术,即ABS编程语言。 从给出的内容可以提取出以下几个...
“开发人员的笔记本”记录了一些准则,技术和想法,这些准则,技术和想法将经典的面向对象的思想应用于Ruby on Rails应用程序。
Technical Notes On Using Data Science & Artificial Intelligence
These are the notes for the Fall 2017 semester version of the Yale course CPSC 202a, Mathematical Tools for Computer Science. They have been subsequently updated to incorporate numerous corrections ...
Lecture notes on introduction Logic 逻辑学导 论 讲义 14th 英文版 Lecture notes on introduct ion Logic 逻辑学导论 讲义 14th 英文版 Lecture note s on introduction Logic 逻辑学导论 讲义 14th 英文 版 教学...
【船级社】 ABS Guidance Notes on Strength Assessment of Independent Type C Tanks 2022-01.pdf
HackNotes Linux and Unix Security Portable Reference is a valuable reference for busy administrators and consultants who value the condensed and practical insight to understanding the threats they ...
"Magic Notes App" 是一个基于JavaScript编写的笔记应用,它旨在提供一个简单、高效的平台,帮助用户记录、管理和分享他们的想法。通过深入理解这款应用的源代码,我们可以了解到JavaScript在构建Web应用方面的强大...
JavaScript技巧积累// 使用vue+axios过程中,防止服务器响应之前,v-text/v-model就渲染空数据content:{ // 手动写入空