我正在学习javascript,现在开始使用对象和基本功能。我遇到了这种类型的代码,并想知道这究竟是什么
var stringFunction = function(){};
stringFunction.test1 = function(){
console.log("Test 1");
}
test1是stringFunction的一部分还是命名约定。提前谢谢
答案 0 :(得分:1)
//pdf generated not given avobe code of how it comes
$pdfdoc = $pdf->Output("confirmation-".$client_code.".pdf", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
$to = $mail_to;
$from = "From: <info@ksilbd.com>";
$subject = "Here is your attachment";
$mainMessage = "BUY/SALE CONFIRMATION";
$fileatt = $attachment;
$fileatttype = "application/pdf";
$fileattname = "confirmation-".$client_code.".pdf";
$headers = "From: $from";
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"-{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$mainMessage . "\n\n";
//base_64
//encoding used
//to encode data
$data = chunk_split(base64_encode($data));
//message
//concat the
//message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
// Send the email
if(mail($to, $subject, $message, $headers)) {
echo "EMAIL SENT SUCCESSFUL.";
}
else {
echo "There was an error sending the mail.";
个实例在Javascript中有点“怪异”,因为它们是对象,但function
为typeof
而不是"function"
。
但是,他们可以使用语法"object"
或f.x
添加和访问属性。您的代码只是向f["x"]
对象添加一个属性(属性的值也是一个函数,但这是无关紧要的。)
答案 1 :(得分:1)
此处test1()
是stringFunction
var的属性(函数类型)
所以你在函数对象中定义了一个函数。
您可以通过调用stringFunction.test1();
来使用它,因为您可以调用外部函数:stringFunction();
var stringFunction = function(){console.log("Test stringFunction")};
stringFunction.test1 = function(){
console.log("Test 1");
}
stringFunction();
stringFunction.test1();
输出:
测试stringFunction
测试1
答案 2 :(得分:0)
每个函数都是一个Function Object。见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function。在您的示例中,代码在对象stringFunction上创建属性“test1”。新属性是itsealf一个函数对象。