Skip to content

Instantly share code, notes, and snippets.

@zinntikumugai
Last active May 29, 2018 14:03
Show Gist options
  • Save zinntikumugai/717ac469eec6e7459c97b9fe00911191 to your computer and use it in GitHub Desktop.
Save zinntikumugai/717ac469eec6e7459c97b9fe00911191 to your computer and use it in GitHub Desktop.

こんなのにreadmeがあるんだろうねぇ..

Node.js(JavaScript) に置いて

var str "hoge";
var data = str;

このままならstr == dataは成り立つが、dataへコピーしたあとに変更を与える場合str == dataになってしまうのでご注意

var str "hoge";
var data = str;
// str == data
str = "";
// str == data

まぁ参照コピーとかなんかあたりをggって見て(投げ

ちなみに

let args_2 = JSON.parse(JSON.stringify(args));  //JSでの値コピー(配列)

で回避できる(配列などは一度JSONにして戻すと一般的なコピーと同等になる)

let     array           type
args   [ 'tip', 'znyt', '1', '<@123456789>' ] object
args_1 [ 'tip', 'znyt', '1', '<@123456789>' ] object
args_2 [ 'tip', 'znyt', '1', '<@123456789>' ] object

command = args.shift()
args   [ 'znyt', '1', '<@123456789>' ] object
args_1 [ 'znyt', '1', '<@123456789>' ] object
args_2 [ 'tip', 'znyt', '1', '<@123456789>' ] object
let str = "./tip znyt 1 <@123456789>";
let prefix = "./";
let args = str.slice(prefix.length).trim().split(/ +/g);
let args_1 = args; //Cで言うところのアドレスコピー
let args_2 = JSON.parse(JSON.stringify(args)); //JSでの値コピー(配列)
console.log("let\tarray","\t\ttype");
console.log("args ", args, typeof args);
console.log("args_1", args_1, typeof args_1);
console.log("args_2", args_2, typeof args_2);
console.log("");
let command = args.shift();
console.log("command = args.shift()");
console.log("args ", args, typeof args);
console.log("args_1", args_1, typeof args_1);
console.log("args_2", args_2, typeof args_2);
console.log("");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment