Tech Junkie Blog - Real World Tutorials, Happy Coding!: JavaScript Functions: Use Properties As Arguments

Thursday, December 2, 2021

JavaScript Functions: Use Properties As Arguments

To make your functions developer friendly it is sometime useful to define your a arguments as properties.  This way your code is self documented, instead of having the developers read documentation on the code you can just use the arguments to document the code so that it would reduce the confusion from the developer.

    <script>

        var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        var copiedNumbers = [];

        function copyNumbers(args) {
            copyAll(args.orignal,
                args.copy,
                args.size);

            function copyAll(original, copy, size) {
                for (var i = 0; i < size; i++) {
                    copy[i] = original[i];
                }
            }
        }

        copyNumbers({ orignal: numbers, copy: copiedNumbers, size: numbers.length });

        console.log("original " + numbers);
        console.log("copies " + copiedNumbers);

    </script>










The code above encapsulates the copyAll function inside the copyNumbers function, which performs a for loop to copy an array of numbers.  Since the arguments object is an object, it can have properties.  So we pass in properties instead of parameters to make the code more readable.  However, the code is not as efficient as a direct call to the function.  Just like anything in programming there's a trade-off.  It's up to you, whether you are willing to make that trade-off.


5 comments:

  1. 토토사이트 이용을 하다 보면 먹튀 피해에 대한 두려움이 갑자기 생기거나 두려움을 가지고 이용하는 경우가 대반사입니다. 토토마추자는 그러한 두려움을 없애드리기 위해 만들어진 먹튀검증커뮤니티 입니다. 다양한 검증 활동을 통하여 축적된 데이터로 방문해 주시는 모든 회원님들께 무료로 제공해드리고 있습니다. 토토사이트를 이용 하는 회원님들은 메이저사이트를 이용하고 싶은 마음이 크다는 걸 알 수 있습니다. 하지만 해당 토토사이트가 메이저인지 아닌지 확인할 방법은 민간인에게 어려움이 있다는 걸 알 수 있습니다. 이용전 꼭 토토마추자를 통하여먹튀검증 서비스를 받아보시기 바랍니다 안전놀이터.
    국내에 합법 토토사이트는 배트맨토토 에서 발매하는 정식서비스만 이용을 할 수 있으나 배팅금액 제한이 있기에 우후죽순 사설토토사이트가 생겨나고 있습니다. 대체적으로 이를 방지를 하려면 배트맨토토 자체에서 배팅금액 한도를 늘리는 수밖에 없다고 생각됩니다.

    ReplyDelete
  2. Check out today’s coronavirus live update, the availability of the nearest hospital ICU bed, and the ambulance contact number in Covid Surokkha. Visit: ambulance service dhaka

    ReplyDelete
  3. Bright Achievements has been proven to be effective in successfully treating children with ABA Therapy NJ for Children with Autism. ABA Therapy in New Jersey Combined with the expert care and guidance of our trained BCBA/LBAs and therapists, your child can achieve more than you thought possible.

    Because of its methods of behavior identification and modification, ABA therapy in New Jersey has been proven to be effective in successfully treating children with ASD. Combined with the expert care and guidance of our trained BCBA/LBAs and therapists, your child can achieve more than you thought possible.

    We are Top in-home Autism Therapy Provider in New Jersey, providing ABA therapy to children with autism & their families in New Jersey. Autism Therapy NJ

    ReplyDelete
  4. Freelancers Incubator is a European educational institution specializing in the field of freelancing and fersonal growth. Become a freelancer

    ReplyDelete
  5. Are you searching for the best online reputation management services in india and USA ? Then you are in the right place. Yes, today I’m going to guide with ripoff report, a better solution to permanently remove cheaterland from SERP. Do you want to get rid of the remove ripoff report from search engine ? myreputationvault provides the best ripoff report removal services in india and usa. ensuring a quick solution to remove ripoff reports or negative reviews from Google or other search engines.

    ReplyDelete

Search This Blog