Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

javascript - Google script string ends with ... and then undefined

I'm sure there's a very basic reason for my problem and I hope you'll point it out :) The problem I have is that a string shows up as ending with "..." on the line of creation but when continuing to the next line it turns into "undefined". Here's an example:

var string1 = "sdfsdfsdfsdfsdfsdfsfsfsfsdfsdfsdfsdfsdfsdfsfasdfasfasdflkjasldflasdkjflaksdjflasjdflaksjdflasdkjf";
var string2 = "sdfgsdfglksjdflgkjsdlfgkjlsdfgkjlsdkgjlkajsdlfkjladskjflakjdsflkajsdlfkjasdlfkjalsdkfjlaskdjflkasjdf";
var string3 = "sdfglksfdjlgkjsldfgkjlsdfkjglsdfkjglsdkfjglsdkjglsdkgjlsdfkjglsdkfjglsdkfjglsdkjglsdkfjglsdkfjglskfdjglsdkjfg";
var string = string1.concat(string2,string3); // debug breakpoint at this line shows the string as ending with ...
var length = string.length(); // debug breakpoint here shows "string" as undifiend but length apears to be working fine.

Any other functions I run on this string return an error as the string is undefined. Ideas?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error is not that string is undefined. It's that string.length() is undefined.

Strings don't have a function length() in javascript. They have a property instead .length. The ellipsis you're seeing ... is probably just your console truncating the console output, but the entire string remains. See below.

var string1 = "sdfsdfsdfsdfsdfsdfsfsfsfsdfsdfsdfsdfsdfsdfsfasdfasfasdflkjasldflasdkjflaksdjflasjdflaksjdflasdkjf";
var string2 = "sdfgsdfglksjdflgkjsdlfgkjlsdfgkjlsdkgjlkajsdlfkjladskjflakjdsflkajsdlfkjasdlfkjalsdkfjlaskdjflkasjdf";
var string3 = "sdfglksfdjlgkjsldfgkjlsdfkjglsdfkjglsdkfjglsdkjglsdkgjlsdfkjglsdkfjglsdkfjglsdkjglsdkfjglsdkfjglskfdjglsdkjfg";
var string = string1.concat(string2,string3); // debug breakpoint at this line shows the string as ending with ...
var length = string.length; // debug breakpoint here shows "string" as undifiend but length apears to be working fine.
console.log(string);
console.log(length);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...