Different ways to add functions to Javascript object

后端 未结 2 1726
温柔的废话
温柔的废话 2021-02-02 01:12

In Javascript is there any difference between these two ways of adding a function to an object? Is one preferable for any reason?

function ObjA() {
    this.A         


        
2条回答
  •  太阳男子
    2021-02-02 01:43

    Using the object constructor will assign a copy of that function to every new instance of your object. Using prototyping will result in one function being shared across all instances.

提交回复
热议问题