Node JS Prototype chain pollution
Before writing
Because of procastinating, I have been for a long time to write some eassy in my blog. But rencently i decide to recover it. So i start to write it down in English.
Prefix Content
With some foundation data, we can acknowledge it more accurately.
The concept of Synchronous and Asynchronous
The Asynchronous like when you are reading, and eating at the same time. Synchronous is you do something one by one without time overlap.
FS in Node.js
so in Node.js, which have a module titled fs, have two function to use: readFileSync() and readFile().(The former is a synchronous function and the latter is an asynchronous function.)
pretending we have a file named dt.txt and the content in it is “DT you should get more exercise”。 like this
and we could use those function to read the content.
Asynchronous :
|
|
Synchronous:
|
|
The module of child_process
This module give us some function to create child process.
Synchronous : spawn exec execFile fork
Asynchornous:spawnSync execSync execFileSync
when we us Asychronous to create process, spawn is others basis, and when we use Synchronous to create it, we can use child_process.spwanSync()、child_process.execSync() and child_process.execFileSync()
Prototype chain
two key word : prototype and proto
prototype
In JavaScript, the prototype object is an important mechanism for implementing object-oriented programming.It is unique to functions. It points to an object from a function. Its meaning is the prototype object of the function, that is, the prototype object of the instance created by this function (in fact, all functions can be used as constructors).
and we can make some example
|
|
as you can see, we can use property: prototype to point to this prototype function to create bar2, and we use food to inherit Food which is a class. so the food have the property : bar2 => 6
proto
As we can concerned, if some class have been instantiated, you wiil not visit its prototype object by function prototype. In this case, we can use function proto to visit it’s prototype object.
so we can do like this :
|
|
Prototype chain’s conception
use the fromer example
|
|
when we print food1.bar and food1.bar1. The Search order is like:
|
|
and this searching structure named prototype chain.
Prototype chain pollution
we use phith0n’s example
|
|
so there is an question: why the property in zoo.bar is 2?? When we output zoo.bar, the node.js engine starts to search in zoo. If it is not found, it searches in zoo.proto, that is, in Object. And, our foo.proto.bar = 2, is to give Object adds a bar attribute, and this attribute is inherited by zoo.This kind of modification of the prototype object of a certain object to control the operation of other objects is prototype chain pollution.