<?php
class TestA
{
public function a() {}
public function b() {}
public function c() {}
}
$a = new TestA();
$a->a()
->b()
->c();
結果是不行的,然後我想了一下,如果把物件自己的參考 ($this) 丟出來不就行了?
1234567891011
<?php
class TestA
{
public function a() { return $this; }
public function b() { return $this; }
public function c() { return $this; }
}
$a = new TestA;
$a->a()
->b()
->c();
<%
Class TestA
Public Function A()
Response.Write "A"
Set A = Me
End Function
Public Function B()
Response.Write "B"
Set B = Me
End Function
Public Function C()
Response.Write "C"
Set C = Me
End Function
End Class
Dim oA : Set oA = New TestA
oA.A().B().C()
%>
這裡是 JavaScript 的動態版:
12345678910111213141516
var TestA = function () {
this.a = function () {
alert('Aa');
return this;
}
this.b = function () {
alert('Ab');
return this;
}
this.c = function () {
alert('Ac');
return this;
}
}
a = new TestA();
a.a().b().c();
然後這是 JavaScript 的靜態版:
123456789101112131415161718
var TestB = function () {}
TestB.a = function () {
alert('Ba');
return this;
}
TestB.b = function () {
alert('Bb');
return this;
}
TestB.c = function () {
alert('Bc');
alert(this.d)
return this;
}
TestB.d = 'abc';
TestB.a().b().c();
// b = new TestB();
// b.a().b().c(); // 這是不能執行的,不能透過物件取用靜態方法。
其實 JavaScript 裡也已經有類似的用法,例如:
123
var s1 = 'ABC';
var s2 = 'DEF';
alert(s1.concat(s2).toLowerCase());