React Native-JS語法簡(jiǎn)單了解

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */


import React,{ Component } from 'react';
import {AppRegistry, Text } from  'react-native';

export default class test extends Component {

  render(){//所喲的類必須有自己的reder方法,用于輸入組件,return里面必須返回一個(gè)組件

      let a = 10;
      var b = 20;

      for (let i=0;i<10;i++){

          let i=30;
          console.log('for i ='+i);
      }

      const foo = {};

      foo.prop = 123;

      console.log('foo.prop = '+foo.prop);

//數(shù)組

      var arr = new Array();//聲明數(shù)組方式一
      arr[1] = 1;//添加數(shù)據(jù)
      console.log('arr ='+arr);//arr= ,1


      var arr1 = new Array(1,3,4);//聲明數(shù)組方式二
      console.log('arr1 ='+arr1);//arr1=1,3,4


      var array = [1,true,2,'abc'];//聲明數(shù)組方式三
      array[4] = 34;//數(shù)組添加數(shù)據(jù)
      array[1] = 54;//數(shù)組改變數(shù)據(jù)
      console.log('array ='+array);//array=1,54,2,abc,34
      array.splice(1,2);//移除數(shù)據(jù),1為開始位置,2為移除的個(gè)數(shù)
      console.log('array移除后 ='+array);//array=1,abc,34
      console.log('aaaa'+array.pop());//移除最后一個(gè)數(shù)據(jù),并返回最后一個(gè)數(shù)據(jù)的值(就是移除的值)34
      console.log('bbbb'+array.shift());//移除第一個(gè)數(shù)據(jù),并返回第一個(gè)數(shù)據(jù)的值(就是移除的值)1


      array.push('ee','ff','gg','hh','jj');//數(shù)組添加多個(gè)數(shù)據(jù)
      console.log('cccc'+array.slice(0,3));//返回0-3的數(shù)據(jù)(以數(shù)組的格式)abc,ee,ff

      console.log('ddddd'+array.concat(array,'hello'));//合并(數(shù)組+數(shù)組、數(shù)組+字符串)返回新的數(shù)組abc,ee,ff,gg,hh,jj,abc,ee,ff,gg,hh,jj,hello



      array = array.join('-');//使用'-'將數(shù)組中的元素隔開組成一個(gè)字符串,返回一個(gè)數(shù)組



      console.log('join===='+array);//array=abc-ee-ff-gg-hh-jj

//字符串
      let string = 'hello';

      console.log(string.charAt(2));//返回字符串給定位置的字符 l

      console.log(string.big());//字體變大
      console.log(string.small());//字體變小
      console.log(string.bold());//字體加粗
      console.log(string.italics());//字體為斜體
      console.log(string.fixed());//
      console.log(string.strike());//中間刪除線
      console.log(string.fontcolor('red'));//字體為紅色
      console.log(string.fontsize(23));//字體大小
      console.log(string.toLowerCase());//小寫
      console.log(string.toUpperCase());//大寫
      console.log(string.link('this is a link'));//添加超鏈接


      console.log('llo第一次出現(xiàn)的位置'+string.indexOf('llo'));//返回第一次出現(xiàn)的位置2
      console.log('字符串是否包含llo===='+string.match('llo'));//包含則返回llo  llo
      console.log('替換字符串llo==='+string.replace(/llo/,'llo world'));//注意replace里面參數(shù)的寫法  hello world



//日期

      var date = new Date();
      console.log(date);//以當(dāng)前時(shí)間為初始值
      console.log('返回當(dāng)前周幾'+date.getDay())//周日、周一、周二、周三、周四、周五、周六 2

      date.setFullYear(2008,8,8);//設(shè)置給定的日期

      console.log('設(shè)置固定的日期'+date);//Mon Sep 08 2008 17:17:19 GMT+0800 (CST)

      date.setDate(date.getDate()+5);//date.getDate()得到當(dāng)前日期,date.getDate()+5是指5天后的日期,date.setDate設(shè)置日期

      console.log('幾天后的日期'+date);//Sat Sep 13 2008 17:17:35 GMT+0800 (CST)

      console.log('返回時(shí)間戳'+date.getTime());//1221297465891


//switch
      let number = 10;
      switch (number){
          case 2:{
              console.log('number is 2');
          }
              break;
          case 4:{
              console.log('number is 4');
          }
              break;
          case 6:{
              console.log('number is 6');
          }
              break;
          default:{
              console.log('number is 10');//number is 10
          }
      }
//while,先判斷再執(zhí)行
      let w = 1;
      while (w <10){
        w++;
      }
      console.log('w經(jīng)過white處理結(jié)果為'+w);//10


//do white,先執(zhí)行一次,在判斷,再執(zhí)行
      let dw = 0;
      do{
        dw++;
      }while (dw < 20)
      console.log('dw do-while后結(jié)果為 '+dw);//20

//break 語句可用于跳出循環(huán)。
//break 語句跳出循環(huán)后,會(huì)繼續(xù)執(zhí)行該循環(huán)之后的代碼(如果有的話)
//continue 語句中斷循環(huán)中的迭代,如果出現(xiàn)了指定的條件,然后繼續(xù)循環(huán)中的下一個(gè)迭代。


      for (let i = 0;i <5; i++){
        if (i == 2)
          break;//跳出循環(huán)
        console.log(i);//0,1
      }

      for (let i = 0;i <5; i++){
          if (i == 2)
              continue;//跳過當(dāng)前判斷,繼續(xù)循環(huán)
          console.log(i);//0,1,2,3,4
      }




//try catch
      try {

      }catch (err){

      }


      
      
      return(

     <Text>Hello World</Text>

    );
      
  }

}

AppRegistry.registerComponent('test' ,() => test);//第一個(gè)test是項(xiàng)目名字,后面的test是當(dāng)前方法的名字(需要運(yùn)行的方法)


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容