prototype of a function can be used to code example

Example 1: javascript prototype vs constructor function

function Class () {}
Class.prototype.calc = function (a, b) {
    return a + b;
}

// Create 2 instances:
var ins1 = new Class(),
    ins2 = new Class();

// Test the calc method:
console.log(ins1.calc(1,1), ins2.calc(1,1));
// -> 2, 2

// Change the prototype method
Class.prototype.calc = function () {
    var args = Array.prototype.slice.apply(arguments),
        res = 0, c;

    while (c = args.shift())
        res += c;

    return res; 
}

// Test the calc method:
console.log(ins1.calc(1,1,1), ins2.calc(1,1,1));
// -> 3, 3

Example 2: react prototype function

class Foo extends Component {
  // Nota: esta sintaxe é experimental e ainda não padronizada.
  handleClick = () => {
    console.log('Clicado');
  }
  render() {
    return ;
  }
}

Example 3: what is the function prototype for fputc()

int fputc(const char ch, FILE *filename);

Tags:

Misc Example