Jump to content
Larry Ullman's Book Forums

raziqijr

Members
  • Posts

    15
  • Joined

  • Last visited

Posts posted by raziqijr

  1. when using functions inside functions the variable names are resolved from scope chain and when objects are inside another object then the property names are resolved through prototype chain , so prototype chain and scope chain are different because scope chain is used for functions and prototype chain is used for objects that why we should use keyword this inside function to refer to object the method is inside , am i correct here .  

  2. var obj {
    var name: 'some value',
    mtd: function () {
    alert(name);
    }
    }

    obj.mtd();


    The mtd function defined in obj has no defined parameters, but when called the this reference and argument list are passed to the function. Is the name property of obj also present in the mtd function so it can be used in the alert() call as above, or does the mtd function have nothing to do with the property named name?

    if lerry ullman answer my question than it would be so great 

  3. Yeah, no problem. The first time I encountered this, I was quite confused as well.

     

    Something I didn't mention in my previous post is that this can also be used in functions called by event handlers.

    For example, if I have the following event handler:

     

     

    someDiv.onclick = changeColor;
     
    function changeColor() {
      
      this.style.background = 'red';
      
    }

     

    Then this in the changeColor function will refer to the JS DOM object someDiv (which likely refers to some div on the screen).

    Note however that this in the above context is not carried over to any other functions called within the changeColor function.

    For example, if we have the following extension of the above code:

     

     

     

    someDiv.onclick = changeColor;
     
    function changeColor() {
      
      anotherFunction();
      
    }
     
    function anotherFunction() {
      
      alert(this);
      
    }
     
    

     

    Then this in the anotherFunction function will not refer to the someDiv DOM object. It will instead refer to the window object.

    To keep the this in the original context, you have to pass this as an explicit argument in the function call.

    thanks again, i understand the use of this keyword , another confusion is say if we have an obj which has some method inside so now the method belong to object that mean we can change obj properties inside the function so why should we use this keyword 

  4.  

    raziqijr, I'm not exactly sure what you're asking (and your JS syntax is incorrect), so I'm going to provide a simple explanation of what this means in JS, and hope that I answer your question in doing so.
     
    If you have a method (i.e., a function) within an object, and you use this within that method, then this refers to the object that contains the method. Expressed graphically, this in the following rudimentary diagram refers to the object that contains the method in which this is being used:
     
    object <-- "this" below refers to this object.
    |
    |__method
       |
       |
       |__this
     
    Now, if you use this in a globally-defined function (i.e., a function not within an object), then this will always refer to the window object. The reason why is because all globally-defined functions are implicitly attached to the window object as methods in JS (and thus a globally-defined function is actually just a method of the window object).
     
    By correcting the syntax in your example, we can observe the following behavior:
    If we execute the following code, then an error will occur, because str is not defined within the sayHello method:
     
    var obj = {
      
      str: 'Hello',
      
      sayHello: function () {
        
        alert(str);
        
      }
      
    }
     
    obj.sayHello();
     
    However, if we change str in the alert function to this.str, then "Hello" is alerted because this refers to the obj variable (which is an object), and this.str will reference the str property in the obj variable:
     
    var obj = {
      
      str: 'Hello',
      
      sayHello: function () {
        
        alert(this.str);
        
      }
      
    }
     
    obj.sayHello();
     
    Does that make sense?
     
    It's worth noting that the above code is kind of pointless because you could easily reference the str property of the obj variable directly, meaning that there's no reason to have the sayHello method at all. For example, the following code would accomplish exactly the same thing:
     
    var obj = {
      
      str: 'Hello',
      
    }
     
    alert(obj.str);
     
    Please let me know if you have any other questions.

    thank you very much HartleySan, so it means that the this keyword can only be used within the function of an object.

  5. !== is the opposite of ===. So if stripos($value, $v) returns anything but false (in other words, the string $v is found in the larger string $value), then the if statement evaluates to true and an empty string is returned.

     

    Basically, this code is something that Larry wrote in order to avoid really bad strings in messages input by the user.

    If any of the strings in the $very_bad array are found in the $value string (which is what the user typed), then an empty string is returned (because the user, either intentionally or unintentionally, typed something that could screw up the system).

     

    ok now read it carefully

     

    The foreach loop will access each item in the $very_bad array one at a time, assigning each item to $v. Within the loop, the stripos( ) function will check if the item is in the string provided to this function as $value. The

    stripos( ) function performs a caseinsensitive search (so it would match bcc:, Bcc:, bCC:, etc.). The function returns a Boolean TRUE if the needle is found in the haystack (e.g., looking for occurrences of $v in $value). The conditional therefore says that if that function’s results do not equal FALSE (i.e., $v was not found in $value), return an empty string.

     

     

    the bold red text above should really have said (i.e , $v was found in $value) not (i.e., $v was not found in $value)

     

    what do u say?

  6. raziqijr, I don't think anyone understood your response, but to test for false, do the following:

     

    if (stripos($value, $v) === false) {
    
    return '';
    
    }

     

    See the following link for details and more examples:

    http://php.net/manua...ion.stripos.php

     

     

    raziqijr, I don't think anyone understood your response, but to test for false, do the following:

     

    if (stripos($value, $v) === false) {
    
    return '';
    
    }

     

    See the following link for details and more examples:

    http://php.net/manua...ion.stripos.php

     

     

    thanks for your assistant , your right i understood your code but could you pls explain this for me

     

     

    foreach ($very_bad as $v) {

     

    if (stripos($value, $v) !== false)

     

    return "";

    }

  7. No, it's correct as written. If the value was not found, in which case the stripos() function returns false, then return an empty string.

     

     

    look sir if stripos() functions result do not equal to false which mean ($v is found in $value) then return empty string. because if stripos() function result is false then it means ($v is not found in $value ) . i am confused pls give me a simple example

  8. this is the the page 405 chapter 13

     

     

    The foreach loop will access each item in the $very_bad array one at a time, assigning each item to $v. Within the loop, the stripos( ) function will check if the item is in the string provided to this function as $value. The

    stripos( ) function performs a caseinsensitive search (so it would match bcc:, Bcc:, bCC:, etc.). The function returns a Boolean TRUE if the needle is found in the haystack (e.g., looking for occurrences of $v in $value). The conditional therefore says that if that function’s results do not equal FALSE (i.e., $v was not found in $value), return an empty string.

     

     

    in above page i bold the text which should really have said (i.e $v was found in $value) am i right pls correct me if i am wrong.

×
×
  • Create New...