几种动态语言(Python/Perl/PHP/Java Script)的比较

 

Perl

Python

Java Script

PHP

变量的语义

A variable is a name for a container that holds one or more values.

Variables are just references to objects.

Variables hold the actual values of primitive types, but they hold only references to the values of reference types.

Variables must be declared first before they are used.

 

var num, str=”aaa”;

标量类型的变量含有具体的值,而复合类型的变量是指向具体对象的指针。

Is everything an object?

No

 

A reference must be blessed before it becomes an object.

Yes

No

 

Variables of primitive types are not objects.

No

 

标量类型的变量不是对象。

Types

Scalars: numbers and strings.

 

But internally, Perl computes with double-precision floatingpoint

values.‡ This means that there are no integer values internal to Perl—an integer

constant in the program is treated as the equivalent floating-point value.

 

Lists and arrays: A list is an ordered collection of scalars. An array is a variable that contains a list. People

tend to use the terms interchangeably, but there’s a big difference. The list is the data

and the array is the variable that stores the data. You can have a list value that isn’t in

an array, but every array variable holds a list (although that list may be empty).

Everything is an object.

 

Variables are just references to objects.

The types can be divided into two groups: primitive types and reference types. Numbers, boolean values, and the null and undefined types are primitive.

 

Objects, arrays, and functions are reference types.

标量类型:boolean, integer, float, double and string.

 

复合类型:array and object

 

特殊类型: resource and null

 

Support constants?

Yes

use constant PI    => 3.14;

 

No constant support in Python.

No constant support in Java Script.

Yes

 

define("PI", 3.14);

Support references?

Yes

 

my @skipper = qw(blue_shirt hat jacket preserver sunscreen);

 

my $ref_to_skipper = \@skipper;

 

Yes

 

Variables are just references to objects.

Yes

 

Objects, arrays, and functions are reference types.

Yes

 

复合类型的变量是指向具体对象的指针。

Call by value or call by reference?

Perl always passes by reference.

 

Please refer to http://stackoverflow.com/questions/5741042/perl-call-by-reference-or-call-by-value.

 

Perl passes by reference. Specifically, Perl aliases each of the argument to the elements of @_. Modifying the elements @_ will change the scalars returned by $a[0], etc and thus will modify the elements of @a.

Arguments are passed by assignment.

 

Arguments are passed by automatically assigning objects to local variable

names.

 

Assigning to argument names inside a function does not affect the caller.

 

Changing a mutable object argument in a function impacts the caller.

Call by value.

 

Similar to Java.

·         Parameters of primitive types are just copied by values. 

·         For parameters of references, the reference itself is copied, so, the parameter will reference the same object as the caller.

·         Changes to  a reference parameter itself will not be visible to the caller.

·         For strings, because they are immutable, it doesn’t matter whether it is passed by value or by reference.

PHP supports passing arguments by value (the default), passing by reference, and default argument values

 

By default, function arguments are passed by value. To allow a function to modify its arguments, they must be passed by reference.

To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition:

 

How to return a value in a function?

Whatever calculation is last performed in a subroutine

is automatically also the return value.

return statement.

 

 

区分函数(有返回值)和过程(无返回值)吗?

No

 No

 

有return语句的就是函数,无return语句的就是过程。

No

 

有return语句的就是函数,无return语句的就是过程。

No

 

有return语句的就是函数,无return语句的就是过程。

Support modules (Dynamically loaded source code files)?

Yes

 

use ABC;

Yes

 

import ABC;

No

 

JavaScript does not have any specific language support for namespaces, but JavaScript objects work quite well for this purpose.

Yes

 

include “abc.php”;

Support namespaces (Logical grouping of names)?

Yes

 

package ABC;

Yes

 

Directory hierarchy reflects package names like Java.

No

 

JavaScript does not have any specific language support for namespaces, but JavaScript objects work quite well for this purpose.

Yes

 

namespace ABC;

Support garbage collection?

Yes

 

Reference-count based GC. So, avid circular reference as much as possible.

Yes

 

Technically speaking, Python’s garbage collection is based mainly upon reference counters,

as described here; however, it also has a component that detects and reclaims

objects with cyclic references in time.

Yes

Yes

 

Reference counter based.

 

A PHP variable is stored in a container called a "zval". A zval container contains, besides the variable's type and value, two additional bits of information.

 

Support Closure?

Yes

Yes

Yes

Yes

 

Please refer to http://php.net/manual/en/functions.anonymous.php.

Support Anonymous Function?

Yes

Yes

 

Lambda expression is the only means creating anonymous functions.

Yes

 

(function() {

    // Code goes here.

})();  

Yes

 

Please refer to http://php.net/manual/en/functions.anonymous.php.

Support Lambda expression?

No

Yes

No

No

支持多重继承?

Yes

 

@ISA = qw( Parent1, Parent2 );

 

ISA数组中可以指定多个父类。

 

方法在父类中的收索顺序是深度优先。

Yes

 

class MyClass(Parent1, Parent2)

 

class 语句可以指定多个父类。

 

For classic classes (the default in 2.X): DFLR

The inheritance search path is strictly depth first, and then left to right—Python

climbs all the way to the top, hugging the left side of the tree, before it backs up

and begins to look further to the right. This search order is known as DFLR for the

first letters in its path’s directions.

 

For new-style classes (optional in 2.X and automatic in 3.X): MRO

The inheritance search path is more breadth-first in diamond cases—Python first

looks in any superclasses to the right of the one just searched before ascending to

the common superclass at the top. In other words, this search proceeds across by

levels before moving up. This search order is called the new-style MRO for “method

resolution order” (and often just MRO for short when used in contrast with the

DFLR order). Despite the name, this is used for all attributes in Python, not just

methods.

No

 

// The constructor function initializes those properties that

// will be different for each instance.

function Rectangle(w, h) {

    this.width = w;

    this.height = h;

}

 

// The prototype object holds methods and other properties that

// should be shared by each instance.

Rectangle.prototype.area = function( ) { return this.width * this.height; }

 

 

每个类只有一个prototype对象。

No

 

class Student extends Person {……}

所有类存在公共的祖先?

Yes

 

UNIVERSAL is the base class from which all objects derive

 

Yes

 

Object is the root class.

Yes

 

In JavaScript, the Object class is the most generic, and all other classes are specialized versions, or subclasses, of it. Another way to say this is that Object is the superclass of all the built-in classes, and all classes inherit a few basic methods from Object.

No

是否支持static方法和属性?

No

 

But because a Perl class often resides in a package. So, you can simulate static properties and methods with package level properties and methods.

 

package Serialize;

 

Readonly my $MAX_DEPTH => 100;

 

my $compaction = 'none';

 

my $depth = $MAX_DEPTH;

 

sub set_compaction {

my (

……

}

Yes

 

1)      For static properties, any properties defined on a Class are static properties shared by all instances.

 

Any properties defined on an instance are instance properties.

2)      For methods, similar things as properties. You can also define methods on a Class as methods shared by all instances.

3)      But, Python supports following three types of methods.

Instance methods, passed a self instance object (the default)

 

Static methods, passed no extra object (via staticmethod)

 

Class methods, passed a class object (via classmethod, and inherent in metaclasses)

No

 

1)      You simulate a class property in JavaScript simply by defining a property of the constructor function itself. For example, to create a class property Rectangle.UNIT to store a special 1x1 rectangle, you can do the following:

Rectangle.UNIT = new Rectangle(1,1);

 

Rectangle is a constructor function, but because JavaScript functions are objects, you can create properties of a function just as you can create properties of any other object.

2)      To define a class method in JavaScript, simply make the appropriate function a property of the constructor.

 

Yes

Magic Methods

After Perl searches the inheritance tree and UNIVERSAL for a method, it doesn’t stop there

if the search is unsuccessful. Perl repeats the search through the very same hierarchy

(including UNIVERSAL), looking for a method named AUTOLOAD.

Please refer to http://www.rafekettler.com/magicmethods.html#access.

 

__getattr__(self, name)

__setattr__(self, name, value)

__delattr__(self, name)

__getattribute__(self, name)

No

Yes

 

Please refer to http://php.net/manual/en/language.oop5.magic.php.

 

 

public void __set ( string $name , mixed $value )

public mixed __get ( string $name )

public bool

是否支持final class?

No

No

No

Yes

 

final class BaseClass {

 

}

This指针的语法

No such keyword.

 

But $self this traditionally used as the variable holding the current object.

No such keyword.

 

But self is traditionally used.

this is a keyword in Java Script.

 

 

$this is a keyword in PHP.

Syntax to create a new object

bless <reference> <package name>

obj1 = Constructor(…)

var obj = new Constructor(…)

$obj = new ClassName(……);

Runtime Type Identification (or introspection) Techniques

Please refer to http://en.wikipedia.org/wiki/Type_introspection.

 

Universal->isa(…)

 

Universal->DOES(…)

 

Universal->can(…)

 

Introspection can be achieved using the ref and isa functions in Perl.

 

 

Please refer to http://en.wikipedia.org/wiki/Type_introspection.

Also, the built-in functions type and isinstance can be used to determine what an object is while hasattr can determine what an object does. For example:

>>> a = foo(10)

>>> b = bar(11)

>>> type(a)

<type 'foo'>

>>> isinstance(a, foo)

True

>>> isinstance(a, type(a))

True

>>> isinstance(a, type(b))

False

>>> hasattr(a, 'bar')

True

 

The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean value. It evaluates to "object" for objects, arrays, and (surprisingly) null. It evaluates to "function" for function operands and to "undefined" if the operand is undefined.

The instanceof has nothing to do with the constructor property. It follows the __proto__ chain instead.

The logic behind obj instanceof F:

  1. Get obj.__proto__
  2. Compare obj.__proto__ against F.prototype
  3. If no match then set temporarily obj = obj.__proto__ and repeat step 2 until either match is found or the chain ends.

In the example above, the match is found at the first step, because: rabbit.__proto__ == Rabbit.prototype.

// defining constructors
function C(){}
function D(){}
 

D.prototype = new C(); // use inheritance

var o3 = new D();

o3 instanceof D; // true

o3 instanceof C; // true

 

Please refer to http://en.wikipedia.org/wiki/Type_introspection.

 

In PHP introspection can be done using instanceof operator. For instance:

 

Class Implementation Details

In general, bless associates an object with a class.

packageMyClass;

my $object = { };

bless $object, "MyClass";

Now when you invoke a method on $object, Perl know which package to search for the method.

If the second argument is omitted, as in your example, the current package/class is used.

For the sake of clarity, your example might be written as follows:

sub new {

  my $class = shift;

  my $self = { };

  bless $self, $class;

}

 

OOP in Python really is mostly about looking up attributes in

linked namespace objects.

The JavaScript Object Oriented paradigm is prototype based, that means that objects "inherit" from other objects, using them as their prototype.

 

PHP的对象是用数组来模拟的

序列化

 

 

 

string serialize ( mixed $value )

 

int file_put_contents (……)

 

string file_get_contents(……)

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值