Am I passing an object.defineProperties object?

I am learning javascript and would like to help understand a piece of code.

From the Object.DefineProperties definition, the first parameter is the object. Is MyObjectConstructor a declaration or an object. With a constructor function, I would expect to call a new one to make it an object.

That's what bothers me. Or, as I read in Javascript functions, these are objects, so I consider it as an object, and the this property is where all the staticProps and instance instances are added to?

var _prototypeProperties = function (child, staticProps, instanceProps) {
   if (staticProps){ 
       Object.defineProperties(child, staticProps)
   };
    if (instanceProps) { 
     Object.defineProperties(child.prototype, instanceProps);
   }
  };

function myFunction() {
    function MyObjectConstructor(element) {
      this.element = element;
      this.initialized = false;
    }

  _prototypeProperties(MyObjectConstructor, {...}, {...});
}
+4
source share
3 answers

Yes, (constructor) functions are also objects in javascript, and you can add properties to them.

_prototypeProperties staticProperties , MyObjectConstructor.myStaticProperty. instanceProps (: " ", " " ) MyObjectConstructor.prototype, : (new MyObjectConstructor).myPrototypeProperty. , MyObjectConstructor "" () , (new MyObjectConstructor).element .initialised.

+1

: function first type object javascript. , . :

function test(){

    return function(){
        console.log('function');
    }
}

test()();

, , - !

var test = function(i) {
    // body...
    return i;
}

test('123');

"test" Anonymous, , .

second: new , construction function, , , .

-: "__proto__" . .

: new, this ! .

:

var _prototypeProperties = function (child, staticProps, instanceProps) {
   if (staticProps){ 
       Object.defineProperties(child, staticProps)
   };
   // the properties from the own construction function  , like this :
   //  this.element = element;
   //  this.initialized = false;


    if (instanceProps) { 
     Object.defineProperties(child.prototype, instanceProps);
   }
  };

  // the properties from the construction function prototype object . like this :
  //   MyObjectConstructor.prototype.name = 'somebody';
  //   MyObjectConstructor.prototype.getName = function(){
  //        return this.name;
  //   }
+1

JavaScript, , :

function Hat() { }
var Hat = function() { }

( new), .

new, , - . new :

  • "it"
  • , "it"
  • "it" this
  • "it". , undefined.

:

// first define Hat function
function Hat() { this.color = 'red' }

// calling with `new` implicitly returns a new object bound to the function
new Hat()
// > Hat { color: "red" }

// invoking without `new` implicitly returns `unefined`,
// but `this` points to Hat parent object.
// If running in the browser, Hat parent object would be `window`
// so now Window.color is set to "red"
Hat()
//> undefined
Window.color
//> "red"

new, .

var color = function() { return "blue" }
color()
//> "blue"
new color()
//> color {}

JavaScript . new , . , , .

Crockford JavaScript: http://javascript.crockford.com/prototypal.html

, 10 -, .

bind, call apply, . , .

+1

All Articles