JavaScript Using “this.prototype” vs outer “Constructor.prototype” in OOP using this in prototype javascript,using prototype board,using prototype

In JavaScript, defining a classlike (constructor) function method is usually done like this:

function Class1() {
   // ... code ...
}
Class1.prototype.method1 = function(args) {
   // ... code ...
};

And going by the general syntax/semantics rules of ECMAScript, there is no reason I couldn't override/alter/extend the original Object.prototype with

function Class1() {
   // ... code ...
   this.prototype.method1 = function(args) {
      // ... code ...
   };
}

Then why is it not done that way? Code would look a bit more confusing, but a lot quicker and cleaner to write and read (putting all Class1-related methods right underneath Class1 itself). Wouldn't it be a better use?

Answer:1

Those two pieces of code are most definitely not the same; the second one won't work.

What happens in the constructor (usually) has nothing to do with the prototype object. It's initialization code for each instance. The "prototype" property of interest is the "prototype" property of the constructor function, not of the instances.

Creating prototype functions inside the constructor doesn't make sense, because it'd mean you'd be re-doing that work for each instance you create.

Answer:2

The problem is those two code samples are not equivalent. In the first example 'this' refers to Class1, but in the second example 'this' refers to whatever that newly created object was. For example,

var myObj = new Class1()

in the example above, 'this' is myObj - which breaks your second example.

Also, even if that did work, you would be then recreating that method for every instance you made. It would be much more efficient to not do that.

Answer:3

As the other answers have pointed out, the second use of prototype is not a valid javascript code, but it seems you want to have access to your constructor when it is actually running, which is possible with these 2 options:

1- if you don't use strict mode, you can have it like:

  var MyClass = function ClassConstructor(){
         arguments.callee.prototype. ...
  }

2- in strict mode for instance if you have your classlike function like:

var MyClass = function ClassConstructor(){
        ClassConstructor.prototype. ...
 }

this way you can use ClassConstructor only in the constructor, it is kind of private there.

Answer:4

I'm started learning JavaScript promises, but I can't understand how apply, for example, Q functions to Node.js callback functions. In similar question is recommended to use Q.denodeify(), but it ...

I'm started learning JavaScript promises, but I can't understand how apply, for example, Q functions to Node.js callback functions. In similar question is recommended to use Q.denodeify(), but it ...

  1. using promises with axios
  2. using promises with react
  3. using promises with jquery ajax
  4. using promises with async await
  5. using promises with fetch
  6. using promises with ajax
  7. using promises with mongoose
  8. using promises with callbacks
  9. using promises with express
  10. using promises with settimeout
  11. using promises with request
  12. using promises with observables
  13. using promises with filereader
  14. using promises with mongodb
  15. using promises with jquery
  16. using promises with dynamodb
  17. using promises with async
  18. using promises with ie11
  19. using promises with node
  20. using promises in javascript

I'm having trouble replacing characters in a string. Here's the code I currently have: var entry_value = document.getElementById("entry_box").value; var length = entry_value.length; for(var l = 0; ...

I'm having trouble replacing characters in a string. Here's the code I currently have: var entry_value = document.getElementById("entry_box").value; var length = entry_value.length; for(var l = 0; ...

  1. replace character string javascript
  2. replace character string python
  3. replace character string in r
  4. replace character string c#
  5. replace character string java
  6. replace character string js
  7. replace character string powershell
  8. replace character string swift
  9. replace character string vba
  10. replace character string typescript
  11. replace character string php
  12. replace character string
  13. replace character string matlab
  14. replace character string bash
  15. replace character string excel
  16. replace character string stata
  17. replace character string sql
  18. replace character string kotlin
  19. replace character string swift 4
  20. replace character string sas

Using angular brings lot of weird style of code. For example I always thought that this <button onclick="myFunction()">Click me</button> style I should not ever use, except when I would ...

Using angular brings lot of weird style of code. For example I always thought that this <button onclick="myFunction()">Click me</button> style I should not ever use, except when I would ...

  1. javascript using function from another file
  2. javascript using function
  3. javascript using function arguments
  4. javascript using function php
  5. javascript using function expression
  6. using javascript function in html
  7. using javascript function in typescript
  8. using javascript function in angular 2
  9. javascript function using parameters
  10. using javascript function in jquery
  11. javascript function using array
  12. using javascript function return value in html
  13. javascript function using ajax
  14. using javascript function in angular 7
  15. javascript function using event
  16. javascript class using function
  17. javascript program using function
  18. javascript object using function
  19. javascript sort using function
  20. javascript using alert function

I want show a popup when I move the mouse over the text for each row of the table. The code I use is this: $.fn.ready(function() { $('#pizzaname').hover(function() { $('#popup'...

I want show a popup when I move the mouse over the text for each row of the table. The code I use is this: $.fn.ready(function() { $('#pizzaname').hover(function() { $('#popup'...

  1. show popup mouseover javascript
  2. show popup on mouseover
  3. show popup on mouseover using jquery
  4. show popup on mouseover bootstrap
  5. show popup on mouseover jquery
  6. show popup on mouseover wpf
  7. show popup on mouseover in asp.net
  8. show popup image on mouseover
  9. show popup div on mouseover
  10. show popup window on mouseover
  11. leaflet show popup on mouseover
  12. jquery show image popup mouseover
  13. angularjs show popup on mouseover
  14. show modal popup on mouseover
  15. javascript show image popup mouseover