Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    May 2017
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to write codes to make object can run sequential function

    Hi:

    I meet a problem about how to run function sequentially,
    the code snippet is as following.

    Code:
     var StackMaker = function() {
       var array = [],
         index = -1;
    
       return {
         push: function(value) {
           array[index += 1] = value
         },
         pop: function() {
           var value = array[index];
           if (index >= 0) {
             index -= 1
           }
           return value
         },
         top: function() {
           var value = array[index];
           return value;
         },
         isEmpty: function() {
           return index < 0
         }
       }
     };
    
    
     stack = StackMaker();
     console.log(stack.isEmpty());
     //=> true
     console.log(stack.push('hello'));
     //=> 'hello'
     console.log(stack.push('JavaScript'));
     //=> 'JavaScript'
     console.log(stack.isEmpty());
     //=> false
     console.log(stack.pop());
     //=> 'JavaScript'
     console.log(stack.pop());
     //=> 'hello'
     console.log(stack.isEmpty());
     //=> true
     console.log(stack.push('hello').top()); //error

    I hope to run stack.push('hello').top(), but it show error messages
    "Uncaught TypeError: Cannot read property 'top' of undefined at window.onload "
    I put the code on https://jsfiddle.net/abramhum/n8xgjLc2/
    Is that possible to run function sequentially, and how to do that,
    Thanks a lot.

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    8,082
    Thanks
    37
    Thanked 1,081 Times in 1,077 Posts
    Even if you gave value a value this thing don't run. It's hog wash. Step by step what are you trying to do.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. #3
    New to the CF scene
    Join Date
    May 2017
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I would like to create function to make the following operation can work, like
    stack.push('hello').top();
    indeed, the upper codes show return hello, but it return error message.
    if divided the operation, like stack.push('hello') then stack.top(), it work,
    but I hope i can execute the function by sequential operations.
    Thanks a lot.

  4. #4
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,820
    Thanks
    26
    Thanked 603 Times in 596 Posts
    What you're looking for is called "chaining".
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  5. #5
    New to the CF scene
    Join Date
    May 2017
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks,
    I found some data like
    https://schier.co/blog/2013/11/14/me...avascript.html,
    but in my case, like codes in https://jsfiddle.net/abramhum/n8xgjLc2/
    How could I make it work like chaining? Thanks a lot.

  6. #6
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,820
    Thanks
    26
    Thanked 603 Times in 596 Posts
    How could I make it work like chaining?
    it's explained in detail in the link you found. you only have to do it.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  7. #7
    New to the CF scene
    Join Date
    May 2017
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I don't know how exactly,that's why I need help

  8. #8
    Regular Coder
    Join Date
    Sep 2010
    Location
    U S of A
    Posts
    291
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Code:
    <script>
    function StackMaker () {
       var array = [],
         index = -1;   
         return{
    	push: function(value) {
           array[index += 1] = value
    	return this;
         },
         pop: function() {
           var value = array[index];
           if (index >= 0) {
             index -= 1
           }
           return value
         },
         top: function() {
           var value = array[index];
           return value;
         },
         isEmpty: function() {
           return index < 0
         }
       }
     };
    sM = new StackMaker;
    alert(sM.push(1).push(2).pop())
    alert(sM.pop())

  9. Users who have thanked Shaka Zorba for this post:

    abramhum (Jun 24th, 2017)

  10. #9
    New to the CF scene
    Join Date
    May 2017
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It works, thanks a lot


 

Tags for this Thread

Posting Permissions

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