python類(lèi)
>>> class Fruit:
def __init__(self,name,color):
self.name=name
self.color=color
>>> a=Fruit('apple','red')
>>> print (a.color)
red
JS類(lèi)
function Animal(name){
this.name=name;
}
Animal.color="black";
Animal.prototype.say = function(){
console.log("I'm " + this.name);
};
var cat1=new Animal("cat");
alert(cat1.name)//cat
不同之處
1.python類(lèi)以關(guān)鍵詞class定義。js則只有在創(chuàng)建實(shí)例時(shí)用new關(guān)鍵字。
2.self參數(shù)和js this參數(shù)異曲同工。