class的静态属性和静态方法

总结先:

class的一般方法定义在类的原型上,实例会继承原型上的所有方法;

class的静态属性和静态方法定义在类上,实例不会继承静态属性和静态方法。访问静态属性或调用静态方法,均通过类名调用。

 

举个例子:

	class Point{
		static staticX = 1;
		static staticY = 1;
		constructor(x,y){
			this.x = x;
			this.y = y;
		}
		toString(){
			return this.x + "," + this.y;
		}
		static toValue(p){
			return p.x+p.y;
		}
	}
	let p = new Point(1,2);    
	let s = p.toString();      //返回"1,2"
	let val = Point.toValue(p);//返回3
	console.log(Point.staticX,Point.staticY); //输出1 1
	console.log(Object.getOwnPropertyNames(Point.prototype));//输出 ["constructor", "toString"]
	console.log(Object.getOwnPropertyNames(p));    //输出["x", "y"]
	console.log(Object.getOwnPropertyNames(Point));//输出["length", "prototype", "toValue", "name", "staticX", "staticY"]

 

如果没有static修饰呢?

class Point{
    x;
    y;
    toString(){
		return this.x + "," + this.y;
	}
    toValue(){
        return this.x+ this.y;
    }
}

const p = new Point();
p.x = 1;
p.y = 2;
console.log(Object.getOwnPropertyDescriptors(Point.prototype));
console.log(Object.getOwnPropertyDescriptors(p));

哦,没有static修饰,就是实例的属性了。

所以,上面也可以这样写:

class Point{
    x;
    y;
    constructor(x,y){
        this.x = x;
        this.y = y;
    }
    toString(){
		return this.x + "," + this.y;
	}
    toValue(){
        return this.x+ this.y;
    }
}
const p = new Point(1,2);

这样是不是更像面向对象编程中类的写法:x,y是属性,constructor是构造函数,toString和toValue是方法。

 

注意哈,与webpack、react搭配使用时,解析class需要安装插件@babel/plugin-proposal-class-properties

//weppack.config.js


const path = require('path');
const HtmlWebapckPlugin = require("html-webpack-plugin");
const {CleanWebpackPlugin} = require("clean-webpack-plugin");
module.exports = {
   mode:'development',
//    devtool:'cheap-source-map',
//    mode:'production',
    devServer:{
        port:3000,
       contentBase:path.join(__dirname,'dist')
    },
    entry:{
        'index':"./src/index.js"
    },
    output:{
        filename:"[name].bundle.js",
        path:path.join(__dirname,"dist"),
        chunkFilename:"[name].bundle.js"
    },
    module:{
        rules:[
            {
                test:/\.js$/,
                include:/src/,
                exclude:/node_modules/,
                use:{
                    loader:'babel-loader',
                    options:{
                        // presets:['@babel/preset-react']
                       presets:[
                           [
                               '@babel/preset-env',{
                                   modules:false
                               }
                           ]
                       ],
                       plugins:['@babel/plugin-proposal-class-properties']
                    }
                }
            },
            {
                test:/\.css$/,
                include:/src/,
                use:['style-loader','css-loader'],
            }
        ]
    },
    plugins:[
        new HtmlWebapckPlugin({
            template:'./index.html'
        }),
        new CleanWebpackPlugin()
    ]
}


 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值