2019年1月17日 星期四

【Ch6物件】 屬性創建/刪除/查詢/列舉 Object.create() & Object.defineProperty()&delete& in & hasOwnProperty()


▌創建物件:{} & Object.create()

  • 括號創建: { }
  • 函式創建: Object.create()
  • Object.create(null) //真的空(連toString都沒有的)
    Object.create(object.prototype) //就是「空物件」,會繼承object.prototype的物件
var o={name:"Joe",age:12}
var o=Object.create({name:"Joe",age:12})
var a=Object.create(null) //完全的null
var b=Object.create(Object.prototype) //像是{}的物件

▌查找與設定:點查詢&字串查詢& Object.defineProperty

  • 點查詢:像是o.name
  • 字串查詢:像是a[“name”] ,該用法的好處在於可以用for迴圈
    var o={}
    for(var i=1;i<10;i++){
    o["room"+i]="編號"+i
    }
    console.log(o.room3)
    
  • Object.defineProperty():它可以同時設定特性的屬性writable、enumerable、configurable
Object.defineProperty(o,"x",{value:"neo",writable:false})

▌刪除特性:delete

  • 刪除(delete):可以刪除特性Property,如果刪除成功會回傳true
  • 刪除只能刪除自有特性,不能刪除繼承特性,但是仍會回傳true
var book={
  author:"Neo",
  age:12
};
delete book.author;
console.log(book.author)    //undefined

▌查詢特性:in & hasOwnProperty()

  • 特性 in 物件:有的會回傳true
  • 物件.hasOwnProperty:有的會回傳true
var o ={x:1}
console.log("x" in o) //true
console.log(o.hasOwnProperty("x"))  //true

▌列舉特性

  • 透過for in 遍歷特性(但是只會遍歷自有屬性,而不會遍歷繼承屬性)
var book={
  author:"neo",
  age:12
}

for (p in book){
  console.log(p) 
}
// author , age

【Ch6物件】 原型鍊是什麼?Object.prototype是什麼?


▌原型鍊是什麼

所有的物件都有一個原型(Prototype)

例如:當new Array的時候,會繼承Array.prototype
例如:當new Object的時候,會繼承Object.prototype
但是Object.prototype是唯一沒有原型的Object
Array會繼承Array.prototype,會繼承Object.prototype
//形成一個原型鍊

▌物件字面值(object literal)

物件字面值是一個運算式,每次運算都會創建新的物件。
var book={  //括號裡面的東西就是字面值
author:"Nissen",
age:21
}

【Ch6物件】 物件是什麼?特性(Property)屬性(Attribute)?物件的來源?


▌物件是什麼

  • 物件是JS的「基礎型別」之一,但是屬於「合成值」
  • 物件是「特性」(Property)的無序集合
  • 每個特性(Property)擁有 名&值 (String-Value)
  • 物件是透過「參考」來操作
var y=x
y可以得到參考值
  • 名是字串,值可以是任何基礎型別
    JS的基礎型別包含:number, string, null, undefined, object

▌特性(Property)

每個物件可以繼承至其他物件的特性(被繼承的物件被稱為Prototype)
物件的特性:
  • 具有名稱(Name)和值(Value)
  • Name可以是是任何字串,甚至是空字串,但Name不能是一樣
  • 值可以是任何基礎型別
var book={   //這個obj擁有2個Property
author:"Nissen",
age:21
}

▌ 屬性(Attribute)

物件有三種屬性:
  • Prototype
  • Class
  • Extensible

▌來源

根據物件的來源,可以把物件分成三種類型
  • Native Object(原生物件):Array、函式、日期、正則表達
  • Host Object(環境物件):瀏覽器提供的
  • User-defined Object(自創物件)
根據屬性的來源,可以把屬性分成三種類型
  • Own Property(自有屬性)
  • Inherent Property(繼承屬性):從原型繼承來的屬性

▌物件可以做的操作

創建
設定
查詢
刪除
測試
列舉

2019年1月15日 星期二

【Python】資料類型操作(Set / List / Typle)


▌集合 set

  • {}是集合
  • A in set 可以判斷,是否有元素A
  • A & B 取交集(list不可以)
  • A | B 取聯集(list不可以)
  • len(a) 取出a的長度
a={1,2,3}
b={2,3,4}
print(1 in a) //true
print(a & b) //2,3
print(a | b) //1,2,3,4

▌可動列表 list

  • [] 是可動的列表
  • A in [] 可以判斷,是否有元素A
  • a[0:2] 取出0~1的元素
  • a[0:2]=[ ] 刪除0~1的元素
  • a[0]=1 把元素0取代 //不可以用來刪除
  • len(a) 取出a的長度
  • a+b //可以合併list
    a=[1,2,3]
    b=[4,5,6]
    print(a[0:2])  //[1,2]
    a[0:2]=[]
    print(a)  // [3]
    a[0]=4
    print(a) // [4,2,3]
    print(a+b) // [1,2,3,4,5,6]
    
問題:如何讓list取交集?

▌不可動列表 Tuple

  • ()是不可動列表
  • 不能使用取代/刪除的功能
a=(1,2,3)
a[0]=2 //失敗

【Python】基本資料類型


最近在學Python,有許多的資料類型是JS沒有的,覺得很有趣

▌基本資料類型

  • 字串:”123”
  • 數字:345
  • 可變列表List:[1,3,4]
  • 不可變列表Tuple:(1,3,4)
  • 集合Set:{1,3,4}
  • 字典:{“apple”:”蘋果”}
  • 布林:True/False

▌字串

  • a*3 //可以重複字串
  • a+a //可以相加字串
  • a[0] //可以取出第一個字
  • “”” 可以換行 “”” //可以換行
a="hello"*3  // hellohellohello
a="hello"+'world'  //helloworld

▌計算

有小數計算:3/6 //0.5
無小數計算:3/6 // 0
餘數計算:7%3 // 1

▌字典

  • 擁有key-value
a={"a":"apple","o":"orange"}
print(a[a]) // apple

2019年1月13日 星期日

【JavaScript】JavaScript原型鏈與繼承/構造函數


▌構造函數

function createobject(name,age){
  this.name=name;
  this.age=age;
  this.talk=function (){
   alert("I am"+this.name+ age)
  }
}  
createobject("Nissen",12);  //this就會變成是跑在window上面
console.log(window.name); //window就會被掛上name

▌原型鏈

  • Prototype是函數的屬性,是一個指向prototype指針
  • Construter是prototype的屬性,是一個指向被實例的指針
  • 原生鍊的製作另外寫Createobject.prototype.talk=function(){}
  • 物件在被new的過程中,會去模仿、繼承原生練擁有的特性
    (像是
function Createobject(name,age){
  this.name = name;
  this.age = age;
}  

Createobject.prototype.talk = function (){
   alert("I am"+this.name+ ","+this.age);
  }

var person=new Createobject("Ken",20);
person.talk()

【JavaScript】奇怪的undefined/null/not defined/NaN的差異在哪?


覺得這是一個不知道可以幹嘛的知識?

undefined

  • undefined是一個數值,他的類型就是undefined(就像123數值類型是number)
  • 當變數被初始化卻沒有被賦值給他時,就會給它特殊的undefined值
    數值的類型包括:undefined/number/object/function/string/boolean
var a //初始化變數a,但沒有給他數值
alert(a) //那就給a一個undefined的值好了
alert(typeof a) //undefined

Null

  • undefined是一個數值
  • Null指向一個沒有值的物件(適合當變數還沒放入特定object的時候使用)
var b = null
alert(a) //null
alert(typeof a) //object

NaN

  • NaN的意思是”not a number”
alert(parseInt("ac")); //NaN
//parseInt應該要回傳數字,但AC無法轉成數字,因此會回傳NaN

Not defined

  • 當根本沒有初始化變數時,就瀏覽器就會報錯,顯示Not defined
alert(c) //a is not defined

【JavaScript】用物件Mapping的方法

If的寫法 我們希望當變數是a時就回傳1,變數是b就回傳2,變數是c就會回傳3,一般寫法就是用if,但是這樣會很冗 ​ // IF style var word if(word == 'a'){ word = 1 } else if...