function obj (name,age){
let sayHello = function(){
console.log(this.name,this.age);
}
return {
naem,//成员名称与变量相同可简写
age,
sayHello,//函数也一样
}
}
obj("java","script");
let obj = {
name:"java",
age:"script",
//下面写法与 fn:function(){ console.log(this.name,this.age);}无区别,只是更方便
fn(){
console.log(this.name,this.age);
}
}
let a = "name";
let b = "age";
let c ="sayHello";
let obj = {};
//想要上面的变量的值,表示对象obj中的属性名,es6前做法
obj[a]="java";
obj[b]= "script"
obj[c]=function(){};
//es6:可以直接在对象中用[]来表示
obj= {
[a]:"java",
[b]:"script",
[c]:function(){}
}
console.log(NaN===NaN);//false 明明两个一样的却不相等
console.log(+0===-0);//true +0,-0计算机存储的二进制不同,却相等
//Object.is就是为了解决这两个问题新增的
console.log(Object.is(NaN,NaN));//true
console.log(Object.is(+0,-0));//fales
let obj1 ={a:1,b:2};
let obj2 ={a:2,b:2,c:4};
//相同于浅克隆
let obj3= Object.assign(obj1,obj2);//obj2合并到obj1中,然后返回obj1
console.log(obj1 == obj3);//true
//另外还可以用es7的...
let obj4 ={
...obj1,
...obj2
}
let a={a:1};
let b ={b:2};
//obj1.__proto__=obj2
Object.setPrototypeOf(obj1,obj2);
function Person(name,age){
this.name = name;
this.age = age;
}
//如果代码量多,总是XX.prototype.XX下去,会造成不易阅读
Person.prototype.show = function(){}
function Person(name,age){
this.name = name;
this.age = age;
}
//如果代码量多,总是XX.prototype.XX下去,会造成不易阅读
Person.prototype.show = function(){}
let pr = new Person();
for(let prop in pr ){
console.log(prop);
}
class Person {
constructer(name,age){//相当于es5中的构造函数
this.name = name;
this.age = age;
}
show(){
}
}
let pr = new Person("java","script");
let sh =new pr.show(); //类的所有方法都是无法被当作构造函数使用
for(let prop in person){
console.log(prop);//不会遍历原型上的方法
}
console.log(wid)
let methedName = "show";
class Person {
constructer(name,age){
this.name = name;
this.age = age;
}
[methedName](){
}
}
class Person {
constructer(name,age){
this.name = name;
this.age = age;//直接赋值就可以,下面方法会运行
//如果作es5的Object.defineProperty
//参数分别为对象,属性名,配制属性
Object.defineProperty(this,"age",{
set(val){},
get(){}
})
}
//创建一个age属性,并给它加上getter,读取该属性时,会运行该函数
get age (){//没有参数
return this.age +"岁";
}
//创建一个age属性时,并给它加上setter,给该属性赋值时,会运行该函数
set age(age){//参数
if(age<0){
age = 0;
}
else if(age>1000){
age = 1000;
}
}
show(){
}
}
class Person {
constructer(name,age){
this.name = name;
this.age = age;
}
static sex = "男";
static weight = 120;
static mehtod(){}
show(){
}
}
console.dir(Person);
//取静态成员
Person.medthod();
Person.sex
person.weight;
class Text(){
a = 1;//相同于在constructer构造函数中的this.a
constructer(){
//this.a = 1;//实例成员是固定值,可以换一种写法,直接在constructer外写
}
}
注意:
class Text(){
method = ()=>{};
//上面这种写法相同于
constructer(){
this.method = ()=>{}//this指向的是Text对象
}
}
const obj = class {//匿名类
a = 1;
b = 2;
}
class Text{
@Obsolete
show(){
console.log("show已过时");
}
}
//看不到效果
funtion Obsolete (target,methodName,descriptor){//对象,方法名,描述
console.log(target,methodName,descriptor);
}
如果两个类A和B,如果可以描述为:B是A,则A和B形成继承关系
如果B是A,则:
//过去继承写法
function Animal(type,name,age,sex){
this.type = type;
this.name = name;
this.age = age;
this.sex = sex;
}
Animal.prototype.print = function(){
console.log(this.type,this.name,this.age,this.sex);
}
function Dog(name,age,sex){
//借用父类的构造函数
Animal.call(this,"狗",name,age,sex);
}
Object.setPrototypeOf(Dog.prototype,Animal.prototype);//将Dog的隐式原型设置成Animal
let dog = new Dog("旺财",3,"male");
//es6继承写法
class Animal{
construtor(type,name,age,sex){
this.type = type;
this.name = name;
this.age = age;
this.sex = sex;
}
}
class Dog extends Animal{
construtor(name ,age,sex){
//调用父类的构造函数
super("狗",name,age,sex);//或者Animal.call(this,"狗",name,age,sex);
}
}
新的关键字:
extends
:继承,用于类的定义super
直接当作函数调用,表示父类构造函数注意:es6中规定如果类是子类必须在construtor中第一行手动调用父类的构造函数
如果子类不写contrutor,则会有默认的构造器,该构造器需要参数和父类一致,并且自动调用父类构造器
//子类不定construtor构造函数的默认情况
class Animal{
construtor(type,name,age,sex){
this.type = type;
this.name = name;
this.age = age;
this.sex = sex;
}
}
class Dog extends Animal{
//默认构造器
//这种情况下,如果子类构造函数参数与父类不同,则会出现参数错乱的情况
construtor(type,name,age,sex){
super(type,name,age,sex);
}
}
class Animal{
construtor(type,name,age,sex){
this.type = type;
this.name = name;
this.age = age;
this.sex = sex;
}
}
class Dog extends Animal{
construtor(name ,age,sex){
super("狗",name,age,sex);
}
}
let dog = new Animal("狗","旺财",3,"male");//逻辑不正确,不应该用Animal类创建一个Dog类,Animal类是抽象的
//解决方法
class Animal{
construtor(type,name,age,sex){
if(new.target === Animal){//利用new.target
throw new Error("你不能直接创建Animal的对象,应该通过子类创建");
}
this.type = type;
this.name = name;
this.age = age;
this.sex = sex;
}
}
class Dog extends Animal{
construtor(name ,age,sex){
super("狗",name,age,sex);
}
}
let dog = new Dog("旺财",3,"male");
let dog = new Animal("狗","旺财",3,"male");//出错