首页 > 解决方案 > 为什么这个构造方法无效?

问题描述

为什么这会给我一个错误:“未捕获的 TypeError:Parent.child 不是函数”

function Parent () {
	this.child = (num) => {
		console.log(num);
	}
}

Parent.child(4);

标签: javascriptfunctionconstructor

解决方案


试试这个方法

     function Parent () {
    	this.child = (num) => {
    		console.log(num);
    	}
    }
    
    const  p = new Parent();
    p.child(4);


推荐阅读