0

I have created var template but when I console.log in chrome console nothing shows up. I am a little confused because if I remove the console.log('template created'); and move it above the var template = function() it renders in the console

    var Template = function(){
      // -------------------------------------------------------------------------
      this.__construct  = function(){    
          console.log('template created');
      };
      // -------------------------------------------------------------------------
      //works in console


      console.log('template created');

      var Template = function(){

      // -------------------------------------------------------------------------
      this.__construct  = function(){

      };
      //-------------------------------------------------------------------------
   };
6
  • Have you executed the function ? Commented May 21, 2015 at 16:16
  • Are you using a JavaScript library? __construct has no special meaning in JavaScript. Therefore, you have to execute the function for it work. Commented May 21, 2015 at 16:17
  • this is what i have here but its from a 2013 tutorial so i dont know if its changed but i cant see it being changed var Template = function(){ // ------------------------------------------------------------------------- this.__construct = function(){ console.log('template created'); }; // ------------------------------------------------------------------------- // ------------------------------------------------------------------------- }; Commented May 21, 2015 at 16:18
  • If you do Template.__construct() it should log to the console. I think you are expecting it to automatically work when you construct the Template object, which is not the case. JavaScript has no notion of __construct as I've stated that's a PHP thing I believe. Also, the // characters indicate a comment so they are not necessary. Can you place the link to the tutorial? Commented May 21, 2015 at 16:21
  • jream.com/dashboard/course/view/2/74 its a payed tutorial so i dont think you will get access but it was on youtube until the guy put copyright claim in youtube ... Commented May 21, 2015 at 20:06

1 Answer 1

4

Are you expecting the console.log in __construct to fire? Cause it won't. What you're doing with

  this.__construct  = function(){    
      console.log('template created');
  };

is creating a function on a new object ( if you're using Template as a function constructor, i.e. new Template() ). What you end up with is a new object with the function __construct. I.e.

var Template = function(){
  console.log('fires when initialized');

  this.__construct = function () {
    console.log('inside construct');
  };
};

var template = new Template(); // 'fires when initialized'
template.__construct(); // 'inside construct'

It's not recommended to create functions on objects in this fashion. You'd typically want to place these functions on the prototype. I recommend reading up on function constructors for a deeper understanding. http://tobyho.com/2010/11/22/javascript-constructors-and/ seems like a good place to start.

Sign up to request clarification or add additional context in comments.

1 Comment

you see that works fine your code Template = new template() is what i want to achieve the tutorial i am following is videos from 2013 i think there code works in video with var Template = function() { this.__construct = function() { console.log('Template created'); }; }; thanks i will work along with what you provided

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.