A few questions about JavaScript

Recently, I have been looking deeply into JavaScript to fully understand the language and ask some egregious questions that I cannot find answers to (in particular, with object-oriented programming).

Assuming the following code:

function TestObject()
{
    this.fA = function()
    {
        // do stuff
    }

    this.fB = testB;

    function testB()
    {
        // do stuff
    }
}

TestObject.prototype = {
    fC : function 
    {
        // do stuff
    }
}

What is the difference between functions fAand fB? Do they behave the same in volume and potential ability? Is this just a convention, or is it technically better or right to some extent?

If only there will ever be one instance of an object at any given time, will the function add to the prototype, for example fC, even cost? Is there any use for this? Is a prototype really useful when working with many instances of an object or inheritance?

"" , TestObject.prototype.functionName = function(){} ?

, JavaScript , , . Java PHP - , JavaScript, , .

+5
4

fA fB

. (fA) (fB) - ( , , ). , , .

, , , . "" "" .

- , , fC, ?

. , , , , , , "". (, Array.prototype.each, ).

"" ...

. - , , , , . :

 Constructor.prototype.method = function(){…}

. :

myLib.extend(Constructor.prototype, {
    method: function(){…}
});

, .

, , . , , , , , , .

+6

fA fB , .

, -, , :

var o = {
   fA: function () { ... },
   fB: function () { ... },
   fC: function () { ... }
};

, , , , , , .

, , . , , , , . , .

,

TestObject.prototype.functionName = function () { };

. , , intial.

+3

: , , ,

...
func();
...    
function func () { ... }

var func = function () { ... };
...
func();
...

,

function TestObject () {
  this.fA = function() { // do stuff };   
  this.fB = testB;    
  function testB() { // do stuff }
}

function TestObject () {
    var testB = function () { // do stuff };    
    this.fA = function () { // do stuff };
    this.fB = testB;
}
0

- JavaScript , JavaScript ?

, javascript " JavaScript -" . , . , , , . JS.

"" , TestObject.prototype.functionName = function() {} ?

, JS MVC (, Spine.js, ). , em.. , JS , . , (, , , - ). , .

, , :

// define a new Class
var AClass = Class.create({
   // Object members (aka prototype), 
   // usually an initialize() method is called if it is defined 
   // as the "constructor" function when a class object is created
}, {
   // Static members
}); 

// create a child class that inherits methods and attributes from parent
var ChildClass = AClass.extend({
   // Child object members
},{
   // Child static members
}); 

AClass.include({
   // Object amendments (aka mixin), methods and attributes 
   //   to be injected to the class object
},{
  // Statics amendments, methods and attributes to be 
  //  injected as class static members
});


// check object type
var aObj = new AClass();
aObj instanceof AClass; //  true
aObj instanceof ChildClass; //  false


var cObj = new ChildClass();
cObj instanceof AClass; //  true
cObj instanceof ChildClass; //  true
0

All Articles