跳到主要內容

ECMAScript中認識到的一些用法

最近看到一些使用 ECMAScript 的使用文章,在這裡做個記錄, 也許幫助自己記憶也能幫助別人(會用到Arrow Function,需要對 Arrow Function 先了解)

1. 建立新物件 & 新陣列的方法

未給值:
var Object = new Object( ) 或寫成 var Object = { }
var NewArray = new Array( ) 或寫成 var NewArray = [ ]

給值
var Object = {firstName: Scott, lastName: Wang, age: 31, };
var NewArray = [11,22,33,44 ]

2. 函數中未包含參數,但透過 arguments 搞定
var Add = ( ) => { alert(arguments.length); alert(arguments[2];}  // 3 45
Add(23,45,12);

3. 利用陣列當做參數計算 (利用apply )

var Add = ( ) => {
 var sum;
 for (var i = 0, j = arguments.length; i < j; i++) {
        sum += arguments[i];
}

Add.apply(null, [2,3,4,5]);  // 14

4. 函式附著到物件上( attach Function)

Circulate(Num1, Num2)
{
   return {
       first: Num1,
       last:  Num2,
       Add: ( ) => { this.first + this.last },
       Sub: ( ) => { this.first - this.last }
  }
}

Circulate cc =  Circulate(100, 25)
cc.Add( )  // 125
cc.Sub( )  //   75


5. 依上例,利用 prototype 加入新的函數

cc.Multi( )  // Type Error

Circulate.prototype.Multi = ( ) => { return this.first * this.last } // 2500


6. 利用 Closures 特性記憶 property 

var Result = (Num1) =>{return (Num2) => { return Num1 + Num2}}

X = Result(9)

X(8) // 17


7. Map 的用法及介紹

var s = new Map( );
obj = {name: Wang}
s.set(o, "Content");
console.log(s.get(obj));  // "Content"

// 鍵值可以是下列各種形式
s.set(22, "hello");
s.set("Name","hello1");
s.set(undefined, "hello2");
var func= function() {console.log("hello");}
s.set(func, "hello ES");  // 函數也行 ^^..

8. Set 的用法及介紹
var s = new Set( );
[1,2,3,4,2,1,3,5].map(x => s.add(x))
for (i of s)
console.log(i); // 1,2,3,4,5 只會有唯一的值,不會重覆

s.add(8);  //
s.has(3); // true
s.delete(2);
s.has(2); // false
s.clear( ); // 全部清除

9. 函數本身可以設定默認值:

var func = (xx = 10) => ({id:xx, name:"Wang"});

var sss = func( )
console.log('sss = ' + sss.id); // 10


10. for ...in 獲取鍵名, for ...of 可以直接取值
var ary = [11,22,33];
for (i in ary) console.log(i); // 0, 1, 2
for (i of ary) console.log(i); // 11,22,33

11. 可以使用 class 並允許定義建構式 (Constructor)
      class Person{
        constructor(n,a,y)
        {
            this.name = n;
            this.founder = a;
            this.year = y;
        }
    }

    var sss = new Language("Wang", "WW", 23);
    alert(sss.name);  // "Wang"

12. 使用 extends 繼承 class

class 同上,我們可以用extends 來繼承 Person class 範例如下

class AnotherPerson extends Person{
constructor(x, y, z, address){
display(x, y, z);
this.address = address;
}
record( ){
   //...
   display.record( );
}
}

參考連結:
http://javascript.ruanyifeng.com/advanced/ecmascript6.html

13. Generator & yield 取代了 promise

在 function 後面加上一顆* 裡面就可以使用 yield 成為 Generator , Generator 對我的認知而言有點像是部份執行,還可再退出後再度執行,因此可以做到非同步的效果。

function* doMaker( )
{
    var index = 0;
    while(index < 3)
        yield index++;   // yield 有鎖住執行的效果,達到非同步的作用
}

var gen = doMaker( );

console.log(gen.next( ).value);  // 0
console.log(gen.next( ).value);  // 1
console.log(gen.next( ).value);  // 2
console.log(gen.next( ).value);  //  undefined








留言

這個網誌中的熱門文章

C# 中 List 複製內容到另一個 List 的幾種方法

簡單又實用 ^ ^ * 第一種透過 .ToList( ) 方法:  List<string> List1 = new List<string>();  List<string> List2 = new List<string>();  List1.Add("A");  List1.Add("B");  List1.Add("C");  List1.Add("D"); List2 = List1.ToList( ); * 第二種方式在建立物件時複製前一個 List  List<string> List1 = new List<string>();  List1.Add("A");  List1.Add("B");  List1.Add("C");  List1.Add("D"); List<string> List2 = new List<string>(List1); * 第三種方法新的List 中使用 AddRange( ) 方法  List<string> List1 = new List<string>();  List<string> List2 = new List<string>();  List1.Add("A");  List1.Add("B");  List1.Add("C");  List1.Add("D"); List2.AddRange(List1); 另可使用 List1.GetRange(Start, End) 來複製不固定大小(但不包含End)的內容

無效的回傳或回呼引數。已在組態中使用 或在網頁中使用 <%@ Page EnableEventValidation="true" %> 啟用事件驗證

寫手機網頁 使用 Client 端的 jquery mobile 時 button 都會比較長,但改用 asp::Button 時發現按鈕大小卻變了,W3C 建議使用 jquery mobile Button 時,儘量使用<a href="#" data-role="button" > ,因此改用 asp::LinkButton 後果然解決了 Button 長度問題,只可惜按下去後,依然有點淚流滿面如下。後來拜求 google 大大,找到了原因及解法如下: 開啓了資料回傳安全驗證功能(EnableEventValidation 為 true),導致錯誤訊息產生。如果必須要驗證資料,則必須使用 ClientScriptManager.RegisterForEventValidation 註冊控制項或直接在頁面.aspx上端 <%Page ... EnableEventValidation = "false"> ,那如果全部頁面都不想驗證,那請在 web.config 下找到 <configuration>      <system.web>       <pages  enableEventValidation ="false" >        ....       </page>       .... </configuration> 嗯  博大精深  ^^ 收工回家去............................

SASS基本語法範例

最近想在 ASP.NET 專案使用 SASS 取替自己寫了很久的 CSS(畢竟少打很多字也有更多的彈性),突然才發現 SASS 語法又忘了一大半,不過在 ASP.NET 上好像.sass 不支援 intellisense ,有人會的話也請不吝賜教,這裡留個簡單例子來提醒快半腦殘的自己^^。言歸正傳,SASS 其實最終需要再解析回 CSS 但在寫法上不但支援 SCSS @變數,利用 tab 嵌入以減少使用大括號的寫法,更是讓 SASS 成為攻城獅學會後再也無法接受CSS 的一大要素。下面例子為一個簡單的 TabSelect 並讓其具有 hover 的特性,游標指到時字體Color 改為黃色並向上 1em 顯示.. HTML: <ul>   <li>Hello1</li>   <li>Hello2</li>   <li>Hello3</li>   <li>Hello4</li>   <li>Hello5</li> </ul> SASS: body,html   background: black ul   display: flex   list-style: none   align-items: center   align-content: center   vertical-align: middle   margin: 50px   padding: 12px   width: 850px   height: 150px   li         width: 150px     margin: 5px     padding: 12px     text-align: center     &:nth-of-type(1),     // & 指 parent,這裡就是指 li     &:nth-of-type(2),     &:nth-of-type(3),     &:nth-of-type(4),     &:nth-of-type(5)         color: blue