2021年2月11日木曜日

javascript thisについて

オブジェクト中のthisは、紛らわしい動作をする場合がある。

var message = 'aaaa';
let obj1 = {
    message : 'Hello',
    func1: function(){alert(this.message);}
}

obj1.func1();      // Hello   #thisは、obj1

let func2 = obj1.func1;
func2();           // aaaa    #thisは、window
func2.call(obj1);  // Hello  明示的にthisを指定することができる。

setTimeout(function(){obj1.func1();}, 1000);  // Hello
setTimeout(obj1.func1, 1000);                 // aaaa
setTimeout(obj1.func1.bind(obj1), 1000);      // Hello  bindでthisを決めてしまうこともできる。


callbackにobj1.func1で渡すと、thisは、obj1にならず、windowになる。
"use strict"をつけると
func2();は動作しない。

javascript 入門

 javascriptをためすには、

ファイルindex.htmlをつくる。

<html>

<body>

  <script src="index.js"></script>

</body>

</html>

スクリプトは、index.htmlと同じフォルダーのindex.jsに記述する。
index.htmlをブラウザで開く。

2021年2月9日火曜日

ssh memo

 ssh 公開キーによる認証


キーペアの作成

$ssh-keygen -t rsa
デフォルトでは、~/.sshに公開キー(public key)と秘密キー(private key)が作られる
id_rsa       秘密キー(private key)
id_rsa.pub   公開キー(public key)

設定ファイルの場所
/etc/ssh/sshd_config


ターゲットのホストへのsshキーの転送
ssh-copy-idコマンドを使用するか
パブリックキーをホストに転送し.ssh/authorized_keys にパプリックキーを登録する。
cat id_ras.pub >> .ssh/authorized_keys


2021年2月8日月曜日

ubuntu node.js インストール

正式版 本家

ダウンロードしたbinaryを/usr/local/lib/nodejsに展開してpathを通す。

LTSの確認 nodejsのホームページで確認する。
 VERSION=v14.15.4
 DISTRO=linux-x64
 wget https://nodejs.org/dist/$VERSION/node-$VERSION-$DISTRO.tar.xz
sudo mkdir -p /usr/local/lib/nodejs sudo tar -xJvf node-$VERSION-$DISTRO.tar.xz -C /usr/local/lib/nodejs

~/.profileの最後に下記を追加する。

# Nodejs
VERSION=v14.15.4
DISTRO=linux-x64
export PATH=/usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin:$PATH

profileを更新する。

. ~/.profile

テスト

node -v

npm version

npx -v



APT INSTALL

$ sudo apt install nodejs npm

$ nodejs --version   <== コマンドはnodejs

v10.19.0   <== 古い


node.jsのバージョン管理モジュールをインストール

$ sudo npm install n -g

stableの最新版をインストール
$ sudo n stable
  installing : node-v14.15.4
       mkdir : /usr/local/n/versions/node/14.15.4
       fetch : https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz
   installed : v14.15.4 (with npm 6.14.10)

Note: the node command changed location and the old location may be remembered in your current shell.
         old : /usr/bin/node
         new : /usr/local/bin/node
To reset the command location hash either start a new shell, or execute PATH="$PATH"

$ node --version
v14.15.4

$ sudo apt purge -y nodejs npm
$ exec $SHELL -l

ここまで

参考

$ sudo apt autoremove
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  fontconfig-config fonts-dejavu-core gyp javascript-common
  libauthen-sasl-perl libc-ares2 libdata-dump-perl libdrm-amdgpu1
  libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libencode-locale-perl
  libfile-basedir-perl libfile-desktopentry-perl libfile-listing-perl
  libfile-mimeinfo-perl libfont-afm-perl libfontconfig1 libfontenc1 libgl1
  libgl1-mesa-dri libglapi-mesa libglvnd0 libglx-mesa0 libglx0
  libhtml-form-perl libhtml-format-perl libhtml-parser-perl
  libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl
  libhttp-daemon-perl libhttp-date-perl libhttp-message-perl
  libhttp-negotiate-perl libice6 libio-html-perl libio-socket-ssl-perl
  libio-stringy-perl libipc-system-simple-perl libjs-inherits
  libjs-is-typedarray libjs-psl libjs-typedarray-to-buffer libllvm11
  liblwp-mediatypes-perl liblwp-protocol-https-perl libmailtools-perl
  libnet-dbus-perl libnet-http-perl libnet-smtp-ssl-perl libnet-ssleay-perl
  libnode-dev libnode64 libpciaccess0 libpython2-stdlib libpython2.7-minimal
  libpython2.7-stdlib libsensors-config libsensors5 libsm6 libssl-dev
  libtie-ixhash-perl libtimedate-perl libtry-tiny-perl liburi-perl libuv1-dev
  libvulkan1 libwayland-client0 libwww-perl libwww-robotrules-perl
  libx11-protocol-perl libx11-xcb1 libxaw7 libxcb-dri2-0 libxcb-dri3-0
  libxcb-glx0 libxcb-present0 libxcb-randr0 libxcb-shape0 libxcb-sync1
  libxcb-xfixes0 libxcomposite1 libxcursor1 libxdamage1 libxfixes3 libxft2
  libxi6 libxinerama1 libxkbfile1 libxml-parser-perl libxml-twig-perl
  libxml-xpathengine-perl libxmu6 libxpm4 libxrandr2 libxrender1 libxshmfence1
  libxt6 libxtst6 libxv1 libxxf86dga1 libxxf86vm1 mesa-vulkan-drivers
  nodejs-doc perl-openssl-defaults python-pkg-resources python2
  python2-minimal python2.7 python2.7-minimal x11-common x11-utils
  x11-xserver-utils xdg-utils
0 upgraded, 0 newly installed, 115 to remove and 0 not upgraded.
After this operation, 475 MB disk space will be freed.
Do you want to continue? [Y/n] 

multipass ファイル共有



$ multipass list
Name                    State             IPv4             Image
primary                 Running           10.124.5.162     Ubuntu 20.04 LTS

$ multipass info primary
Name:           primary
State:          Running
IPv4:           10.124.5.162
Release:        Ubuntu 20.04.2 LTS
Image hash:     d68d50a4067d (Ubuntu 20.04 LTS)
Load:           0.00 0.00 0.00
Disk usage:     2.0G out of 4.7G
Memory usage:   150.7M out of 981.2M


$ multipass mount /home/myhome/share primary
$ multipass info primary
Name:           primary
State:          Running
IPv4:           10.124.5.162
Release:        Ubuntu 20.04.2 LTS
Image hash:     d68d50a4067d (Ubuntu 20.04 LTS)
Load:           0.00 0.00 0.00
Disk usage:     2.0G out of 4.7G
Memory usage:   154.5M out of 981.2M
Mounts:         /home/myhome/share => /home/myhome/share
                    UID map: 1000:default
                    GID map: 1000:default

解除
$ multipass umount primary

Ubuntu adduser

adduser username      下記の処理も行ってくれる


useradd 

cp -a /etc/skel/. /home/username        

chown -R username:username /home/username       

usermod -a -G adm,cdrom,dip,lxd,plugdev,sudo username     <==必要なグループを追加する

などなど


$ sudo adduser  xx

ユーザー `xx' を追加しています...

新しいグループ `xx' (1002) を追加しています...

新しいユーザー `xx' (1002) をグループ `xx' に追加しています...

ホームディレクトリ `/home/xx' を作成しています...

`/etc/skel' からファイルをコピーしています...

新しい パスワード: 

新しい パスワードを再入力してください: 

passwd: パスワードは正しく更新されました

xxのユーザ情報を変更中

新しい値を入力してください。標準設定値を使うならリターンを押してください

フルネーム []: 

部屋番号 []: 

職場電話番号 []: 

自宅電話番号 []: 

その他 []: 

以上で正しいですか? [Y/n] y


2021年2月5日金曜日

javascript arguments

 特殊な変数

    arguments  配列のような変数

 配列ではないため、foreachとかpopなどはない

function test() {

    for(let arg of arguments) alert(arg); 

    // or

    //let args = Array.from(arguments); 

    let args = [...arguments];

     args.forEach(arg => alert(arg));

    // arguments.forEach(arg => alert(arg)); これはできない

}

test(1,2,3);


rest parametersは、配列である。

function test(...args) {

    args.forEach(arg => alert(arg)); 

}

test(1,2,3);