Property lookup
Prototype properties
Performance
Prototypes that extend built-in types
Summary
JavaScript does not include the traditional class inheritance model, but uses the prototype prototype model.
is often mentioned as a shortcoming of JavaScript. In fact, the prototype-based inheritance model is more powerful than traditional class inheritance. Implementing the traditional class inheritance model is easy, but implementing prototypal inheritance in JavaScript is much more difficult. (It is for example fairly trivial to build a classic model on top of it, while the other way around is a far more difficult task.)
JavaScript is the only widely used language based on prototypal inheritance. So it takes some time to understand the difference between the two inheritance models.
The difference is the way JavaScript uses prototype chain inheritance.
function Foo() { this.value = 42; } Foo.prototype = { method: function() {} }; function Bar() {} // 设置Bar的prototype属性为Foo的实例对象 Bar.prototype = new Foo(); Bar.prototype.foo = 'Hello World'; // 修正Bar.prototype.constructor为Bar本身 Bar.prototype.constructor = Bar; var test = new Bar() // 创建Bar的一个新实例 // 原型链 test [Bar的实例] Bar.prototype [Foo的实例] { foo: 'Hello World' } Foo.prototype {method: ...}; Object.prototype {toString: ... /* etc. */};
In the above example, the test object inherits from Bar.prototype and Foo.prototype; therefore, it can access the prototype method of Foo. At the same time, it can also access the Foo instance property value defined on the prototype. Note that new Bar() does not create a new Foo instance, but reuses the instance on its prototype; therefore, all Bar instances will share the same value property.
Property lookup
When looking for a property on an object, JavaScript traverses up the prototype chain until it finds a property with a given name.
When you reach the top of the prototype chain - that is, Object.prototype - but the specified property is still not found, undefined will be returned.
Note: Do not use Bar.prototype = Foo, as this will not execute Foo's prototype, but will point to function Foo. So the prototype chain will go back to Function.prototype instead of Foo.prototype, so the method will not be on Bar's prototype chain.
Prototype attribute
When the attribute is used to create a prototype chain, any type of value can be assigned to it (prototype). However, assigning the atomic type to the prototype will be ignored.
function Foo() {} Foo.prototype = 1; // 无效
Assigning the object to prototype, as shown in the above example, will dynamically create the prototype chain.
Performance
If a property is at the upper end of the prototype chain, it will have a negative impact on the search time. In particular, attempting to access a property that does not exist will traverse the entire prototype chain.
And, when using a for in loop to traverse the properties of an object, all properties on the prototype chain will be accessed.
Extending prototypes of built-in types
One wrong feature that is often used is to extend Object.prototype or other prototype objects of built-in types.
This technique is called monkey patching and breaks encapsulation. Although it is widely used in some JavaScript libraries such as Prototype, I still don't think it is a good idea to add some non-standard functions to the built-in types.
The only reason to extend built-in types is to be consistent with new JavaScript, such as Array.forEach.
Summary
Before writing complex JavaScript applications, fully understanding how prototype chain inheritance works is a must for every JavaScript programmer. Beware of performance issues caused by long prototype chains, and know how to improve performance by shortening the prototype chain. Furthermore, never extend prototypes of built-in types except for compatibility with new JavaScript engines.
Note: This is a commonly used method in the programming field, called Backport, which means adding new patches to the old version.
The above is the content of JavaScript Advanced Series - Prototype. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
