TypeScript笔记 javascript

 

// 循环

// for...in 一组值的集合或列表进行迭代输出

var j;

var n = "a b c";

for (j in n) {

console.log(n[j],j);//a,0 1 b,2 ....

}

// for...of 允许你遍历 Arrays(数组), Strings(字符串), Maps(映射), Sets(集合)等可迭代的数据结构等

let someArray = [1, "string", false];

for (let entry of someArray) {

console.log(entry); // 1, "string", false

}

//因为 forEach 在 iteration 中是无法返回的,所以可以使用 every 和 some 来取代 forEach

let list = [4,5,6];

// list.forEach((val, idx, array) => {

// // val: 当前值

// // idx:当前index

// // array: Array

// console.log(val,idx,array);

// });

// var arr = [1,2,3,4,5];

// var num = 3;

// arr.every(function(v){

// if(v == num) {

// return false;

// }else{debugger

// console.log(v);

// return true;

// }

// });

// for(var i=0;i<arr.length;i++){

// if(i==2){continue;}

// console.log(arr[i])

// }

list.every((val, idx, array) =>{//迭代时每一次都得有返回,返回ture是继续,返回false是结束。无返回,则只循环首个

console.log('eT',val);//4

return true;//continues 4 5 6

})

list.some((val, idx, array) =>{//返回true,结束

if(val==5){

return true;//break

}

console.log('sT',val);//4

})

list.some((val, idx, array) =>{//返回false,跳出本次循环,继续

if(val==5){

return false;//continues

}

console.log('sF',val);//4 6

})

list.some((val, idx, array) =>{//无返回,循环全部

console.log('sAll',val);//4 5 6

})

// 函数参数

//在 TypeScript 函数里,如果我们定义了参数,则我们必须传入这些参数,除非将这些参数设置为可选,可选参数使用问号标识 ?

// function buildName(firstName: string="gates", lastName?: string) {}//传参时, firstName 必选的,lastName 可选;参数不能同时设置为可选和默认

var myFunction = new Function("a", "b", "return a * b"); //构造函数

var x = myFunction(4, 3);

console.log(x);

var foo = x=>10 + x ;//单个参数 () 是可选的;无参数时可以设置空括号

console.log(foo(100))

//函数重载

//如果参数类型不同,则参数类型应设置为 any

// function disp(x:any,y?:any):void {

// console.log(x);

// console.log(y);

// }

// disp("abc") ;//abc undefined

// disp(1,"xyz");//1,xyz

var str = new String( "This is string" );

console.log("str.constructor is:" + str.constructor);//对创建该对象的函数的引用

var str1 = new String( "This is beautiful string" );

var index = str1.localeCompare( "This is beautiful string"); //用本地特定的顺序来比较两个字符串

console.log("localeCompare first :" + index ); //相同输出0,不同输出1

 

var str="The rain in SPAIN stays mainly in the plain";

var n=str.match(/ain/g);//查找找到一个或多个正则表达式的匹配 ["ain", "ain", "ain"]

 

// \w 表示 匹配包括下划线的任何单词字符(含数字)

var re = /(\w+)\s(\w+)/;

var str = "zara ali";

var newstr = str.replace(re, "$2, $1");//$1,$2...是表示的小括号里的内容

console.log(newstr); // ali, zara

//slice()截取片段 split()字符串拆分为数组

//substr()从起始索引提取指定书目字符 substring()提取两个索引之间的字符

//valueOf()返回指定字符串对象的原始值 var str = new String("Runoob"); console.log(str.valueOf( )); // Runoob

 

//数组

// 数组方法

// every() 检测数值元素的每个元素是否都符合条件

//some()检测数组元素中是否有元素符合指定条件。

function isBigEnough(element, index, array) {

return (element >= 10);

}

var passed = [12, 5, 8, 130, 44].every(isBigEnough);

console.log("Test Value : " + passed ); // false

var retval = [2, 5, 8, 1, 4].some(isBigEnough);

console.log("Returned value is : " + retval ); // false

 

//map() 通过指定函数处理数组的每个元素,并返回处理后的数组。

var numbers = [1, 4, 9];

var roots = numbers.map(Math.sqrt);

console.log("roots is : " + roots ); // 1,2,3

 

//reduce()将数组元素计算为一个值(从左到右)。reduceRight()从右到左

var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; });

console.log("total is : " + total ); // 6

 

//TypeScript元组

//元组中允许存储不同类型的元素,元组可以作为参数传递给函数。

 

//接口不能转换为 JavaScript。 它只是 TypeScript 的一部分


 

//类

// class Car {

// engine:string; // 字段

// constructor(engine:string) { // 构造函数

// this.engine = engine

// }

// disp():void { // 方法

// console.log("发动机为 : "+this.engine)

// }

// }

//子类除了不能继承父类的私有成员(方法和属性)和构造函数,其他的都可以继承;不支持继承多个类,但 TypeScript 支持多重继承

//类继承后,子类可以对父类的方法重新定义,这个过程称之为方法的重写。 super 关键字是对父类的直接引用,该关键字可以引用父类的属性和方法

// class PrinterClass {

// doPrint():void {

// console.log("父类的 doPrint() 方法。")

// }

// }

// class StringPrinter extends PrinterClass {

// doPrint():void {

// super.doPrint() // 调用父类的函数

// console.log("子类的 doPrint()方法。")

// }

// }

//访问控制修饰符 public(默认) protected自身、子、父 private自身

//类可以实现接口,使用关键字 implements,并将 interest 字段作为类的属性使用

 

//声明文件 declare 定义的类型只会用于编译时的检查,编译结果中会被删除。声明文件以 .d.ts 为后缀

 

With this digital Early Release edition of Programming JavaScript Applications, you get the entire book bundle in its earliest form—the author's raw and unedited content—so you can take advantage of this content long before the book's official release. You'll also receive updates when significant changes are made, as well as the final ebook version., Take your existing JavaScript skills to the next level and learn how to build complete web scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this book, you’ll learn how to write flexible and resilient code that’s easier—not harder—to work with as your code base grows., JavaScript has become one of the most widely used—and essential—programming languages for the Web, on both the client-side and server-side. In the real world, JavaScript applications are fragile, and when you change them things often break. Author Eric Elliott shows you how to add features without creating bugs or negatively impacting the rest of your code during the course of building a large JavaScript application., Early Release content now available:, AMD, Asynchronous Operations, Callbacks, Promises and Deferreds, Code Quality, Function Polymorphism, Function Scope, Hoisting and Closures, Functional Programming and Stateless Functions, Immediately Invoked Function Expressions, Interfaces, JavaScript Style Guide, Lambdas, Method Chaining and Fluent APIs, Method Context, Named Parameters, Node Modules, Object Factories, Partial Application and Currying, Plugins, Principles of Modularity, Prototypal Inheritance, Prototype Cloning and the Flyweight Pattern, The Module Pattern, Unit Testing, Coming soon:, Architecting for Scale, Collaboration, Build, Continuous Integration, Deployment, Communicating with Servers and APIs, Designing and Programming RESTful APIs with Node.js, Event-driven, Modular Client Side Application Architecture, Feature Toggles, Internationalization, Logging and Cross Cutting Concerns, Separatian of Concerns (MVC, etc.)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值