关键字: if else if else == 比较内容是否相等 / 较数据类型是否一致 不一致就会强制类型转换 === 比较内容是否相等 & 前面为false 后面也会执行 && 并且前面为true 后面也得为true | 或 无论前面是否为true 都执行后面的判断 || 或者 前面为true 后面则不执行 % 取模 // == 比较数据类型是否一致 不一致就会强制类型转换 // typeScript中 '' 空字符串 , 0 数字0 ,'null' null,undefined 都会被认定为false ,其他为true
switch(key) case value : break 基本等同于Java / JavaScript
- // @ts-nocheck
- // @ts-nocheck
- import UIAbility from '@ohos.app.ability.UIAbility';
- import hilog from '@ohos.hilog';
- import window from '@ohos.window';
- import AbilityConstant from '@ohos.app.ability.AbilityConstant';
-
- export default class EntryAbility extends UIAbility {
- onCreate(want, launchParam) {
- let age = 18 //隐式声明一个number类型
- let state = true //隐式声明一个boolean类型
- if (age % 2 === 0) {
- // == 比较数据类型是否一致 不一致就会强制类型转换
- //===
- // typeScript中 '' 空字符串 , 0 数字0 ,'null' null,undefined 都会被认定为false ,其他为true
-
- console.log("偶数");
- } else {
- console.log("奇数");
- }
-
- //if 如果...条件成立 &&并且. 后面为true 则执行
- if (age > 18 && age < 66 && state) {
- console.log("符合条件满18 并且小于66 并且 state 为true");
- }
-
- if (age > 0) {
- console.log("age > 0");
- }
- else if (age < 0) {
- console.log("age < 0");
- } else {
- console.log("age = 0");
- }
-
- let name = undefined
- if (name) {
- //如果不是 undefined , null , 'null' , '', 0 则会执行
- }
-
- let value = 'A'
- switch (value) {
- case 'A':
- console.log('isA')
- break
- case 'B':
- console.log('isB')
- break
- default:
- console.log('isOther')
- break;
- }
-
- let index = 0
- while (index < 10) {
- index++
- console.log(index.toString());
- }
-
- let names = ['A', 'B']
-
- //普通循环
- for (let i = 0; i < names.length; i++) {
- console.log(names[i]);
- }
-
- //forEach 根据索引
- for (const i in names) {
- console.log(names[i]);
- }
-
- //forEach 根据值
- for (const name of names) {
- console.log(name)
- }
-
- //names.forEach()
-
- //function 定义方法
- function eat(name: string): void {
- console.log("eat" + name)
- }
-
- function add(a: number, b: number): number {
- return a + b
- }
-
- //等同于 add
- function add1(a: number, b: number) {
- return a + b
- }
-
- eat('apple')
- console.log(add1(1, 2).toString());
-
- //简写版function
- let sayHi = (name: string) => {
- console.log('Hi' + name);
- }
-
- let getValue = (value: number) => {
- return value++
- }
-
- sayHi('summer')
- console.log(getValue(1).toString())
-
- //传参 可选参数
- let sayHi2 = (name?: string) => {
- name = name ? name : 'test'
- console.log(name);
- }
-
- //不传参 使用默认参数
- let sayHi3 = (name: string = 'World') => {
- name = name ? name : 'test'
- console.log(name);
- }
-
-
- //枚举
- enum T {
- T1 = "T1",
- T2 = "T2",
- T3 = "T3"
- }
-
- //枚举 不写值 默认是 1-3
- enum T1 {
- T1,
- T2,
- T3
- }
-
- //接口
- interface IA {
- onClickListener(view: number): void
- }
-
- //定义类 实现接口
- class Iimp implements IA{
- constructor() {
- //构造方法 等同于Kotlin
- }
- onClickListener(view : number) : void{
- console.log("implements method");
- }
- }
-
- let typeA : Iimp = new IA()
- typeA.onClickListener(1)
-
-
- class A1 {
- private valueA1 : number
- private valueA2 : number
- constructor(index : number,index1 : number) {
- this.valueA1 = index;
- this.valueA2 = index1;
- }
-
- public area() : number{
- return this.valueA1;
- }
- }
-
- //extends 继承
- class A2 extends A1{
- constructor(index: number) {
- super(index,index)
- }
-
- }
-
- let a2 : A2 = new A2(1)
- a2.area()
-
- //模块开发 功能抽取 .ts文件 每个文件都是一个模块
- //export 表示可以导出
- export class BaseUtils{
-
- }
- //导出方法
- export function addNumber():void{
-
- }
-
- import {BaseUtils,addNumber} form '../Test2' //引用类 或者方法
-
-
-
-
-
-
- }
- }
-