JavaScript eliminates two ways of whitespace on both sides of a string, object-oriented and functional programming

Source: Internet
Author: User

The main is to eliminate string spaces in JavaScript, to compare the two different ways

// object-oriented, eliminating whitespace on both sides of a string function () {returnthis. Replace (/(^\s*) | ( \s*$)/g, "");}; // to the function of the left and right spaces; function Trim (s) {  return s.replace (/(^\s*) | ( \s*$)/g, "");}
Called to eliminate whitespace in two ways.
var defualtphone = defualtdeliveryarray[0].getelementsbytagname ("TD") [4].textcontent.trim ();
var defualtphone = Trim (Defualtdeliveryarray[0].getelementsbytagname ("TD") [4].textcontent);
is  the difference between ' ABCD ',  tirm () and trim ('  abcd   ').
Since the programming is now intelligent editor, when using object-oriented method as an object property, in the object namespace, it is easy to automatically fill out the full.

Object-oriented methods are given to a certain class of objects, whether in the call or the editor automatic completion of the advantages of too much.

In Python, the difference between functional programming and OOP programming calls is that if there are two types

For a object1 type of thing, it needs to be passed to two function processing. ;

def fun1a (Object1,*args,**kwargs):

Pass

def fun1b (Object1,*args,**kwargs):

Pass

For a object2 type of thing, it needs to be passed to two function processing. ;

def fun2a (Object2,*args,**kwargs):

Pass

def fun2b (Object2,*args,**kwargs):

Pass

When called, use FUN1A (Object1,*args,**kwargs) fun1b (Object1,*args,**kwargs), fun2a (Object2,*args,**kwargs) fun2b (Object2, *args,**kwargs)

Pass the function when must not pass the wrong, cannot pass the Object2 type to the FUN1A and fun1b these two functions processing, will be wrong.

When object-oriented, the method of invocation is:

A = A (*args,**kwargs)

A.FUN1 ()

A.fun2 ()

B = B (*args,**kwargs)

B.FUN3 ()

B.fun4 ()

B.fun5 ()

From the call point of view, the more methods, object-oriented instantiation of the number of parameters can be passed less. For the process, if the FUN4 need to take advantage of FUN3 processing results, but also in the fun3 of various return, as well as fun6 fun7 what, a variety of return is not very good. To keep multiple functions working on the same variable, you have to use some global global variables.

When using object-oriented, because the editor has the intelligent completion of the function, certainly will not appear b.fun1 () This disorderly call of the low-level error, in obtaining the results, you can use the object2.xxx way to obtain, do not go around to return stir global variables.

This article simply describes the advantages of OOP from the form of function/method invocation in JS and py.

JavaScript eliminates two ways of whitespace on both sides of a string, object-oriented and functional programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.