Judge the existence of strings and non-empty methods in JS

How do you think about this topic? This is very simple, TypeOf, then judge Length.

i (TypeOf unknownvariable === ‘string’ && unknownvariable.Length) {…}
   
Did you get it?
What will this string created with new String ()? Typeof This unknown variable is definitely Object. what would you do?

You must have to judge the type first, TypeOf unknownvariable === ‘Object’? But you have to think that there is a null variable TypeOf value is also object. Is that still to be && unknownvariable? Now is a non-empty object, then do you want to judge what Length? Should not be used. But the last TypeOf is Object, it is not necessarily a string object, which can be other objects, such as arrays, JSON objects, New Objects, and more. What should we do?

This is to be rarely used but useful methods: valueof. Valueof is output in the form of a variable original type.

Let str1 = “test” let str2 = new string (‘test2’) let arr1 = [1, 2, 3] let fn1 = function () {Console. LOG (‘this is a function’)} let Obj1 = {name: ‘gpd’} let Obj2 = new object () obj2.name = ‘gpd’ str1.valueof () // “TEST” STR2.Valueof () // “test2” arr1.valueof () // [1, 2, 3] fn1.valueof () // fn1 () {Name: “gpd”} Obj2.valueof () // {name: “GPD”}

So, whether you are a string of string or new string get a string object, it Valueof value is a string of string. Then, its TypeOf value is String.
 So the last judgment is   
IF (TypeOf unknvariable! = Undefined && unknowvariable && typeof unknowvariable.valueof () === ” “) {…}


© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment