7/23/2007

Javascript string replacing function (2)

0 comments

Previously I mentioned a string replacing function that, instead of building a string this way:

tag= '<' + tagname + ' style="background-color:'
+ bgcolor + ';color:' + color + '">' + text + '</' + tagname +'>'

allows for doing it this way:

tag0= '<___ style="background-color:___;color:___">___</___>'
tag1 = tag0._(tagname, bgcolor, color, text, tagname)

or this way :

tag00= '<[.tagname.] style="background-color:[.bgcolor.];'+
'color:[.color.]">[.txt.]</[.tagname.]>'

data = {tagname:'span', bgcolor:'#EEFFBB',color:'red',txt:'tag2'}
tag2 = tag00._(data)


After playing with the regular expression for a while, I come up with a short version :

String.prototype._=function(){
var i=-1, a= arguments
var t= typeof(a[0])=='string'
var rx = t?(/___/g):(/\[\.[A-Za-z0-9_]*\.\]/g)
var f = t?function(x){i++;return a[i]}
:function(x){return a[0][x.slice(2, -2)]||''}
return this.replace(rx,f)
}


To me it's amazing to see how such a task can be accomplished with such a neat code.

7/21/2007

Problems in Javascript Subclassing

0 comments

In javascript, the subclassing is done this way:

[1] function Person(name, sex){    # Define the superclass
[2] this.name = name
[3] this.sex = sex
[4] this.prn = function(){ return this.name + ', '+ this.sex }
[5] }

[6] function Man(name){ this.sex = 'M' } # Define the subclass
[7] Man.prototype = new Person() # Connect Person and Man

And the instantiation:
[8]  p = new Person('Jane','F')
[9] m = new Man('John')

Checking the function prn (defined in [4]):
p.prn() ==> Jane, F
m.prn() ==> undefined, M


As shown, the name John given to Man when instantiating m ([9]) is NOT transferred to m. It is obvious 'cos we didn't instruct it to do so when defining Man in [6].

Problem.1: Subclass Arguments Are Not Automatically Transferred To Subclass Instances



So, unless explicitly instructed, the arguments of a subclass will not be assigned to the instance of that subclass. That means, [6] needs to be re-defined as:
function Man(name){ 
this.sex = 'M'
this.name= name # <== new line
}

Note that the above highlighted line already exists in the superclass (line [2]). Why does it have to be re-done again in the subclass ? Didn't we already instruct Person to be the super class ?

Problem.2: An Instance of Subclass Is an Instance of the Superclass too ?


Lets investigate the instanceof operator in javascript:
[10] (p instanceof Person)  ==> true
[11] (m instanceof Man) ==> true
[12] (m instanceof Person) ==> true


Line [10] and [11] are what we expect. But [12]? How come m is an instance of Person ? This doesn't sound right.

Problem.3: The Constructor of A Subclass' Instance Is The Superclass But Not the Subclass ?


Check the constructor of the instances of subclass and of superclass:
[13] getConstructor(p) ==> Person
[14] getConstructor(m) ==> Person

Shouldn't the constructor of m be Man?

Solution Proposal



Lets try a new way of defining a subclass. Instead of
function Man(name){ 
this.sex = 'M'
this.name= name # <== new line
}

We try this:
function Woman(name){ update(this, new Person(name, 'F'))}
w= new Woman("Jan")

See the following table for comparison:

ClassesInstanceinstanceof
Person
constructor
function Person(name, sex){
this.name = name
this.sex = sex
this.prn = function(){
return this.name +','+this.sex}
}
ptruePerson
function Man(name){
this.sex = 'M'
this.name= name }
Man.prototype = new Person
mtruePerson
function Woman(name){
update(this, new Person(name,'F'))
}
wfalseWoman

7/20/2007

中國的食品污染

0 comments

今天讀到大紀元的一篇報導:毒牙膏再演鬧劇 質檢總局大變臉
http://epochtimes.com/b5/7/7/20/n1778129.htm

該文提到因中國外銷的含毒牙膏所引發的一連串新聞。文中提到的「二甘醇」是一種抗凝劑,若水中含有二甘醇,則在低溫時會防止水分子結成冰。二甘醇的含量越高,越不易結冰,可能在零下二、三十度時都還保持液體狀態不會變成冰塊。中國製的牙膏,就是利用二甘醇的這種特性,使其即使保存很久也不會硬化。這是對人體有害的有毒物質,因此像牙膏這種不小心會吃進人體的東西不應該含有二甘醇。

其他重點:

  1. 中國生產的幾種牙膏含有0.21%至7.5%的二甘醇。
  2. 中國衛生部不久公佈了該部「專家」的評估結論:「長期使用二甘醇含量低於15.6%的牙膏不會對人體健康產生不良影響」,但報告未顯示專家姓名,也沒說明該試驗的具體方法。
  3. 7月16日,中國質檢總局局長公開批評境外媒體對中國出口產品質量問題的報導是"顛倒黑白",他表示,中國食品合格率要高於美國。
  4. 中國工廠即使自己想要合法生產也很困難,因為他們購買的材料本身就是非法的有毒物(可能買不到無毒的)。譬如說,生產用的水,前幾年由於污染的加劇,中國政府不是採取措施防治並減少污染,反而降低水質標準,把以前的污染水當成合格水賣給企業
  5. 今年七月,據中國質檢總局對食品、化肥和農用機械等眾多消費品質量抽查,結果發現19.1%的產品達不到標準。其中最嚴重的是食品問題,特別是罐裝食品、乾果和果汁飲料。
  6. 中國常用油鹽醬醋米麵的合格率平均低於40%,特別是低價食品,其衛生質量根本無法保障。
  7. 中國進口到美國的食品只有 0.9% 有被抽查。即使在如此低的抽樣率下,中國裝船的食品每個月有 200 次在港口被拒收,排名世界第一。
  8. 中國食品藥品質量監測管理局2005年批准了1113個新藥,這數目等於美國近十年來新藥的「受理」總量(看清楚,不是批准量)。

About Subclassing in Javascript

0 comments

Let the superclass be:

function Person(sex){alert('I am Person')}

and subclasses:

function Man(){this.sex='M'}
function Woman(){this.sex='F'}

The following two subclassing approaches don't seem to make any difference:

Man.prototype = new Person
Woman.prototype = new Person()
m = new Man()
w = new Woman()

In both cases:
  • The function Person will be executed
  • The constructor of both m and w are Person