The buffer Slice () method returns a new buffer and the original buffer refers to the same object!!! Change must pay attention to!

Source: Internet
Author: User
Note that the ***********
//slice () method returns a new buffer, but the new BF2 and source object BF refer to the same .... That is, the change of BF2 will change the Bf
var bf=new Buffer (' Panwen ');
Console.log (BF);   <buffer 6e 6e>
var bf2=bf.slice (1,4)//<buffer 6e 77>---intercept from subscript 1, intercept to 4, excluding 4
Console.log (BF2);
bf2[0]=0;
Console.log (BF2);  <buffer 6e 77>
console.log (BF)//<buffer 6e 6e>//********
Note ***********
To achieve deep copy, use Buffer.copy (TEMBF);
 
var bf=new Buffer (' Panwen ');
Console.log (BF);   <buffer 6e 6e>
var bf3=new Buffer (a);
var bf3=bf.copy (); That's not how it's written. , is the following writing//************** realize deep copy.
bf.copy (BF3);//This is copied to BF3. , the copy is also optional, bf.copy (bf3,1,2,4) is copied from the second to 3, 1 in real copy to the first bit of buffer.
Console.log (BF3);//<buffer 6e 6e 6f 00> after four-bit random, not initialized

bf3[0]=0;
Console.log (BF3);//<buffer 6e 00>
console.log (BF)//<buffer 6e 6f 6e>, No. Will change the original object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.