Skip to main content
Background Image

Node JS Prototype chain pollution

·631 words·3 mins· loading · loading ·
Table of Contents

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 :

var fs =  require('fs');
fs.readFile('dt.txt',function(err,data){

	if(err){
		return console.error(err);
	}

	console.log("Asynchronous read:"+data.toString());
});

Synchronous:

var fs =  require('fs');
var data = fs.readFileSync('dt.txt');
console.log("Synchronous read: " + data.toString());

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

function Food(bar,bar1,bar2) {
  this.bar = 1;
    this.bar1=5;
}
let food = new Food();
Food.prototype.bar2=6;
console.log(food.bar1);
console.log(food.bar2);
//5
//6

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 :

function Food(bar,bar1,bar2) {
    this.bar = 1;
    this.bar1=5;
}
let food = new Food();
console.log(Food.prototype===food.__proto__);

Prototype chain’s conception
#

use the fromer example

function Food() {
    this.bar = 1;
    this.bar1=5;
}
function food(){
    this.bar=2;
}
food.prototype = new Food();
let food1 = new food();
console.log(food1.bar);
console.log(food1.bar1);

when we print food1.bar and food1.bar1. The Search order is like:

first we search in it original class, it hava property bar=2
so it can print it like : 2

and we want to print property bar1 in it, but as we see, it not appear in its class, and we define food.prototype = new Food(), so it will search in it's prototype object ,
so it will print : 5

and this searching structure named prototype chain.

Prototype chain pollution
#

we use phith0n’s example

// foo是一个简单的JavaScript对象
let foo = {bar: 1}

// foo.bar 此时为1
console.log(foo.bar)

// 修改foo的原型(即Object)
foo.__proto__.bar = 2

// 由于查找顺序的原因,foo.bar仍然是1
console.log(foo.bar)

// 此时再用Object创建一个空的zoo对象
let zoo = {}

// 查看zoo.bar
console.log(zoo.bar)

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.

Delete's blog
Author
Delete’s blog