SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array.prototype.push = function() {

    I found this code on Internet:
    Code:
     
    <script type="text/javascript">
    
    Array.prototype.push = function() {
        var n = this.length >>> 0;
        for (var i = 0; i < arguments.length; i++) {
            this[n] = arguments[i];
            n = n + 1 >>> 0;
        }
        this.length = n;
        return n;
    };
    
    </script>
    Question 1: How to call this function?
    Question 2: When I delete >>> 0, what I lose? ( >>>0 ı sildiğim zaman, ne kaybederim?)

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That code is a way to add the .push method to the native Array object in Javascript.

    All A-grade browsers have this method already built-in, so the script you're showing is designed to allow for older browsers to also have this method available.

    To use the push() method of the array object:
    Code:
    var a = ["apples","oranges"];
    a.push("bananas"); //a = ["apples","oranges","bananas"]
    HTH
    var me = null;

  3. #3
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks...
    I add message
    Code:
     
    <script type="text/javascript">
    
    Array.prototype.push = function() {
    alert(this.length); // 4
        var n = this.length >>> 0;
        alert(n); // 4
        alert(arguments.length);  // 3
        for (var i = 0; i < arguments.length; i++) {
        alert(arguments[i]);
            this[n] = arguments[i];
            n = n + 1>>> 0;
        }
        this.length = n;
        return n;
    };
    
    var arr = ["aa","bb","cc","dd"];
    arr.push("ee","ff","gg");
    alert(arr); // aa,bb,cc,dd,ee,ff,gg
    </script>
    I delete >>> 0
    Code:
     
    <script type="text/javascript">
    
    Array.prototype.push = function() {
    alert(this.length); // 4
        var n = this.length ;
        alert(n); // 4
        alert(arguments.length);  // 3
        for (var i = 0; i < arguments.length; i++) {
        alert(arguments[i]);
            this[n] = arguments[i];
            n = n + 1 ;
        }
        this.length = n;
        return n;
    };
    
    var arr = ["aa","bb","cc","dd"];
    arr.push("ee","ff","gg");
    alert(arr); //  aa,bb,cc,dd,ee,ff,gg
    </script>
    Both of them display same messages. I think, >>> 0 is unnecessary.

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its basically IE 5 that didn't support push, so there is really no need to use that script.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you are going to use it you should first check that push doesn't exist so as to not override the more efficient version built into almost all browsers.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •