2018年3月29日 星期四

Bit wise

OperationResultSame asResult
5 & 110101 & 0001 0001
5 | 150101 | 0001 0101
~ 510 ~0101 1010
5 << 1100101 << 1 1010
5 ^ 140101 ^ 0001 0100
5 >> 120101 >> 1 0010
5 >>> 120101 >>> 1 0010
OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
<<Zero fill left shiftShifts left by pushing zeros in from the right and let the leftmost bits fall off
>>Signed right shiftShifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
>>>Zero fill right shiftShifts right by pushing zeros in from the left, and let the rightmost bits fall off

2018年3月25日 星期日

建構函數建構物件

建構函數可以用來定義 物件的屬性和方式:

 <p id="output"></p>

第一步:建立物件屬性

function mycar(name,ID,size,age){
        this.name=name;
        this.ID=ID;
        this.size=size;
        this.age=age;
};

name, ID, size, age的參數是用來建立物件的屬性值, this 指的是物件本身.

第二步:建立物件
利用new來建立物件.
var car= new mycar('Jason',3345678,'33cm',26); //這樣的方式是 mycar 直接傳入物件的屬性值.

or
var car=new mycar();
car.name="Jason";
car.ID=3345678;
car.size="33cm";
car.age=26;

第三步:建立物件方法

function mycar(name,ID,size,age){
        this.name=name;
        this.ID=ID;
        this.size=size;
        this.age=age;
        this.print=printCar;

};

function printCar(){
       document.getElementById('output').innerHTML=this.name+'<br>'+this.ID+'<br>'+
       this.size+'<br>'+this.age;

};

car.print();


output:

Jason
3345678
33cm
26


2018年3月24日 星期六

JS string-method(slice(), substring(), substr())

如何提取出部分的string?

string 有三種方式(method):


1. slice(x,y): x y參數表示起始位置與結束位置,在這範圍內的字串會被提取出來
   x y可以正可以負
  如果沒有設定第二個參數的話, x位置以後的字串就會被直接提取
input:
    <p id="output"></p>

    <script>
      var text1="abcdefgh" ;
      var str=text1.slice(1,3);
     

      document.getElementById('output').innerHTML=str;
    </script>

output: bc


2. substring(x,y):類似slice method 但是其值必須是正值

3. substr(x,y): 類似slice method 但其第二個參數代表“長度”

input:
    <p id="output"></p>
   
    <script>
      var text1="abcdefgh" ;
      var str=text1.substr(1,3);
     
      document.getElementById('output').innerHTML=str;


    </script>

output: bcd 


01234
|a|b|c|d|
-4-3-2-10

JS-string method (indexOf() lastIndex())

String 的 Method

String length:
input:












output:










語法:

string.length, length為string的屬性,可以幫忙計算出字串的長度
(array也有這樣的用法,不過是用來算出有多少個item)。

在字串中尋找字串:

"indexOf(''....")" "search('......')"

這兩種有所不同之後可以再做詳細的探討。

這樣的方式可以回傳 符合('.....') 內的第一個發生的字串第一個字母的 '位置'

input:

 <script>
    var x ="ABCDEFGH"
    var y =x.indexOf('D');
    console.log(y);  


    </script>

output: 3

D所在位置 在字串的第四個位置,為什麼輸出是3 ?
因為在JS 裡 第一個位置的index為0!

lastIndexOf()


input:
 <script>
    var x ="ABCDEFGHDHDABVS"
    var y =x.lastIndexOf('D');
    console.log(y);  


    </script>

output:10

尋找最後一個出現D的位置
如果沒有符合尋找的條件 indexOf() /lastIndexOf() 會回傳 -1的值 。

indexOf('....',參數) 其中參數表示從該位置往後尋找符合的條件
input:
<script>
    var x ="ABCDEFGHDHDABVS"
    var y =x.indexOf('D',7);
    console.log(y);  

    </script>
output:8