2020年1月22日水曜日

Javascript function, passing variable or value

let's see the below function

function update(o) {
  o.a = o.a + 1;
  o = { a: 100 };
}

and do the following test ,

>const obj = {a:1} 
>update(obj)

console.log(obj)

what is the result ?

it would be 2.

> console.log(obj.a)

1


note that
even if the object is const value, the object keys are not protected.

in the function update(o), first line of o.a is still seeing the value of the obj, which is out of the scope of the function,
then second line, o = {a:100} , which is replacing o in the scope of function to a new object, however obj  in the outside of the function is not changed.

Javascript NaN, comparing or not

NaN is not a number and would appear when you calculate number.

>const result = "foo" * 2;

it will give you NaN as result.

however this

>console.log(result === NaN);

it will print false

because

NaN compares unequal (via ==, !=, ===, and !==) to any other value -- including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.


>Number.isNaN(result)
>isNaN(result)

will return true


Xcode. device is not connected.

When Xcode said  your device was not connected but actually it was connected.
it would be solved reboot however avoiding reboot, type the below command.


sudo launchctl stop com.apple.usbmuxd
stackexchange

What is launchd

according to wikipedia,
launchd is an init and operating system service management daemon created by Apple Inc. as part of macOS to replace its BSD-style init and SystemStarter. There have been efforts to port launchd to FreeBSD and derived systems.

it could solve other problems too..

2020年1月18日土曜日

Javascript Boolean()

Boolean()
Creates Boolean objects.
The Boolean object is an object wrapper for a boolean value. (ref)


what do you think the output of the following code ?
const bool = "false";
console.log(Boolean(bool)); 

it will print true.

because Boolean() evaluate values which are nil, or undefined to false but other value for example String to true.

const bool = "false";
console.log(bool == false); // false

console.log(Boolean(bool)); // true

also an empty array [] would be evaluated to true

const bool = [];
console.log(Boolean(bool)); // true


Don't confuse Boolean() and boolean primitive like true or false

XQaurtz 日本語表示

macOSXのXQaurtz 日本語表示

.bash_profile
export LANG='ja_JP.UTF-8'
export LC_ALL='ja_JP.UTF-8'
export LC_MESSAGES='ja_JP.UTF-8'
test -r ~/.bashrc && . ~/.bashrc
を追加
アプリケーション->カスタマイズ
ターミナルの起動を xterm -u8に設定

Custom reduce function to Javascript Allay class

learn how to use Javascript prototype.
let's make a reduce() function of  Allay class using  javascript prototype.


Array.prototype.myReduce = function(callback, initVal) { 
 let acc = initVal; 
  for (i = 0; i < this.length; i++) { 
   acc = callback(acc, this[i]); 
  }; 
 return acc
}


let myArray = [1,2,3]
myArray.myReduce((acc, elem) => { return acc + elem; }, 0)
6


What is reduce() function 

from mozira document of reduce

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));

// expected output: 15



2020年1月16日木曜日

ios customise tabbar with UITabBarController

Here is a sample of creating your own UITabBarController and  customise tab bar behaviour on ios. not only customising tint colour of icon or icon itself but also adding more functionality when switching tab, it required to make your own class inheriting UITabBarController but you want to use the views and tab bar controller scene from story board.
  1. start from tab application default
  2. remove segure from story board connected to views from tab bar scene
  3. set story boad ID to each scene which would be navigated by the tab bar. (FirstView, SecondView for example)
  4. create a new class MyTabBarController
    ```
    class MyTabBarController: UITabBarController, UITabBarControllerDelegate {
    var firstViewController: FirstViewController!
    var secondViewController: SecondViewController!
    override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
    firstViewController = storyboard?.instantiateViewController(withIdentifier: "FirstView") as? FirstViewController
    secondViewController = storyboard?.instantiateViewController(withIdentifier: "SecondView") as? SecondViewController
    firstViewController.tabBarItem.image = UIImage(named: "customFirstImage")
    secondViewController.tabBarItem.image = UIImage(named: "customFirstImage")
    viewControllers = [firstViewController, secondViewController]
    selectedIndex = 0
    }
    /*
    // MARK: - Navigation
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
    }
    */
    }
    ```
  5. set MyTabBarController as the class of tab bar controller in the story board