trait Counter {
static $trvar=1;
public static function stfunc() {
echo "Hello world!"
}
}
class C1 {
use Counter;
}
print "\nTRVAR: " . C1::$trvar . "\n"; //prints 1
$obj = new C1();
C1::stfunc(); //prints Hello world!
$obj->stfunc(); //prints Hello world!
trait A
{
private function smallTalk()
{
echo 'a';
}
private function bigTalk()
{
echo 'A';
}
}
trait B
{
private function smallTalk()
{
echo 'b';
}
private function bigTalk()
{
echo 'B';
}
}
trait C
{
public function smallTalk()
{
echo 'c';
}
public function bigTalk()
{
echo 'C';
}
}
class Talker
{
use A, B, C {
//visibility for methods that will be involved in conflict resolution
B::smallTalk as public;
A::bigTalk as public;
//conflict resolution
B::smallTalk insteadof A, C;
A::bigTalk insteadof B, C;
//aliases with visibility change
B::bigTalk as public Btalk;
A::smallTalk as public asmalltalk;
//aliases only, methods already defined as public
C::bigTalk as Ctalk;
C::smallTalk as cmallstalk;
}
}
(new Talker)->bigTalk();//A
(new Talker)->Btalk();//B
(new Talker)->Ctalk();//C
(new Talker)->asmalltalk();//a
(new Talker)->smallTalk();//b
(new Talker)->cmallstalk();//clet numbers = [10, 100, 1000]
let doubled = numbers.map(function(x){
return x * 2
})
document.write(doubled)
Alternative arrow function:let doubled = numbers.map((x) => { return x * 2 })
If it's only a single line we don't need curly brackets and the keyword return let doubled = numbers.map((x) => x * 2);
If the arrow function has only one parameter, it does not need parentheses:let doubled = numbers.map(x => x * 2);
Note: We only need parentheses if we had zero or more than one parameters// Anonymous function
document.addEventListener('click', function(){
console.log('You clicked me!');
})
The same function with arrow function syntax// Arrow function
document.addEventListener('click', () => {
console.log('You clicked me!');
})
Even more minimalistic arrow function// More minimalistic arrow function
document.addEventListener('click', () => console.log('You clicked me!'))driveCar() function using call() function, this will refer to the object that will be passed.
In this case: john objectlet john = {
firstName: 'John',
lastName: 'Doe'
}
function driveCar() {
document.write(this.firstName + ' ' + this.lastName + ' is driving a car<br/>')
}
driveCar.call(john)lastName declared by the keyword let is not added to the global window object.
That is why the variable is equal to undefined // Declaring variable using keyword "var"
var firstName = "Jet"
// Declaring variable using keyword "let"
let lastName = "Li"
// Declaring variable without using any keyword
carMark = "Tesla"
function driveCar() {
console.log(this.firstName + ' ' + this.lastName + ' is driving a car '+this.carMark)
}
driveCar()
The result will be:Jet undefined is driving a car TeslaBecause declaring variable with var keyword and without any declaration keyword are the same.
Both add the variable to the global window object.
After running the above code, global object window will have firstName and lastName attributeswindow.firstName; // "Jet"
window.lastName; // undefined
window.carMark; // "Tesla"this inside driveCar() function refers to the global window object.
Global variables firstName and lastName are added to the window object.
That is why they are visible using the keywordfirstName = "Jack"
lastName = "Ma"
function driveCar() {
document.write(this.firstName + ' ' + this.lastName + ' is driving a car<br/>')
}
driveCar()
If we call the same function using call function, this will refer to the object that will be passed.
In this case: john objectlet john = {
firstName: 'John',
lastName: 'Doe'
}
function driveCar() {
document.write(this.firstName + ' ' + this.lastName + ' is driving a car<br/>')
}
driveCar.call(john)this inside fn() function refers to the global window object.
That is why firstName and lastName attributes are not accessible using this keywordlet john = {
firstName: 'John',
lastName: 'Doe',
driveCar() {
function fn () {
console.log(this.firstName + ' ' + this.lastName + ' is driving a car'); // "undefined undefined is driving a car"
console.log(this);
}
fn(); // fn.call(this);
console.log(this.firstName + ' ' + this.lastName + ' is driving a car');
console.log(this)
}
}
john.driveCar();
In order to fix the issue, one of the ways is to use call function and pass the object that we want this to refer to inside the fn() functionfn.call(this);
Inside driveCar() function this refers to the john object because it is called through john objectjohn.driveCar();