Preface
Today in a front-end group, I saw someone posting a question:
var o = new Object();function foo(obj){ var obj = o; obj.name = '123'; obj = new Object(); obj.name = '456'; } foo(o); console.log(o.name)
As shown in the above code, without changing anything In the case of conditions and data, is it possible to output "456"?
It didn’t take long for people in the group to start drawing conclusions, which they couldn’t do under normal circumstances.
The reason? Anyone who knows a little about how to use reference types knows that new Object() changes the point of obj and can never get 456.
Here comes the problem
It is impossible to output 456 under normal circumstances, so under what error conditions can it be output## Where is #456? After all, programmers are good at writing bugs. They should be able to cause abnormal situations.
Then, my handicapped self suddenly thought of the Proxy I just learned two days ago. We now have a problem when assigning values, so can we solve the problem from metadata? Try it://demo_01var o = new Object(); o = new Proxy(o,{ set(target, key, value,receiver){ if(Object.is('name',key)) return Reflect.set(target, key, `456`,receiver); return Reflect.set(target, key, value , receiver); } })function foo(obj){ var obj = o; obj.name = '123'; obj = new Object(); obj.name = '456'; } foo(o); console.log(o.name)Perfect, successfully outputs
456.
What? Don't you know what a Proxy is? Click here –> ES6 Interceptor, ProxySo, now is the time to show off the results, and prepare to send them to the group to show off. Let’s look through the chat history first. Hehe, someone has already given an answer faster than me. Okay, let’s take a look at other people’s answers first://demo_02var o = new Object();Object.defineProperty(o,'name',{ set(val){ this.value = '456'; }, get(){ return this.value; } })function foo(obj){ var obj = o; obj.name = '123'; obj = new Object(); obj.name = '456'; } foo(o); console.log(o.name)Awesome young man, I thought of going together. They are all working on getters and setters, and they are all processing data at the meta level. This classmate posted an article at the same time, saying that he got inspiration from here. Click here and enjoy it together. It is indeed very well written-> Impeccable hook Next, I will also post my answer and join in the fun at the time. Wait, some people expressed dissatisfaction and said that he was opportunistic. The demo_02 code is completely equivalent to the demo_03 code:
//demo_03var o = new Object();function foo(obj){ var obj = o; obj.name = '123'; obj = new Object(); obj.name = '456'; } foo(o); o.name = '456'; console.log(o.name);When the last line of code "obj.name = '456';" in the demo_02 method is modified, "this.value = '456" in the demo_02 code ';" must also be modified accordingly. That makes sense. We are indeed taking advantage of opportunities. I wrote demo_01 with the same problem. Fortunately, I didn’t click the Enter key, otherwise I would have been embarrassed.
So is there any way to solve this problem and achieve real output?
456
What does the new operator do? What is our problem now? The problem is that it is on new Object(), which modifies the this pointer of obj. So is there any way we can avoid it? What? If you ask me why this pointer has changed, I can only say that you really need to learn JS hard. Take a look at this article. The author's writing style is so interesting. He starts from a soldier and talks about what New has done.
I remember that there is a construct method in the Proxy handler. If I can modify its this in the Object constructor. The idea was good, but it failed and didn't come to fruition. If anyone can continue with this idea, please let me know the answer>_ Checking the chat history, the classmate gave another solution:
var o = new Object();var _Object = Object;Object = function(){ return o; }function foo(obj){ var obj = o; obj.name = '123'; obj = new Object(); obj.name = '456'; } foo(o); console.log(o.name)Object = _Object;This time this time The classmate first rewrote the Object object so that it returns the object o. This avoids the change of this point. You are so talented, I admire you. Silently collect<_>
Work hard, there will always be someone who can teach you something
The above is the detailed content of About new, let's start with a BUG. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools