JS通过'工厂'继承 - undefined - 无法读取属性

时间:2016-06-04 09:34:37

标签: javascript inheritance merge prototype

编辑:这不是关于原型如何运作的问题。问题在于代码排序。我已经在下面添加了脚本。

function copyPrototype(parent,descendant) {
  var parentProto = Object.create(parent.prototype)
  parentProto.constructor = descendant
  descendant.prototype = parentProto
}

function addBird(_staysWinter) {
  let bird = new Bird (_staysWinter)
  return bird
}

function Bird(_staysWinter)  {
  Animal.call (this, 2)
  this.staysWinter = _staysWinter
}
copyPrototype(Animal, Bird)

Bird.prototype.fly = function () {
  alert ('flying')
}

function Animal (_numLegs) {
  this.numLegs = _numLegs
}
Animal.prototype = {
  walk: function () {
    alert ('walking')
  }
}

fluffy = addBird(0)
fluffy.fly()
fluffy.walk()

我被困了...... 为什么以下代码段(jsfiddle)会导致:
 Uncaught TypeError: Cannot read property 'fly' of undefined

这里的想法是我有许多不同类型的动物' (简化)所有人共享某些属性。但是,我不直接想要实例化底层对象。

基本上当我在prototype.fly中输出一个警告输出this时,它会返回undefined。

编辑开始
在我的示例中,我取出了new关键字,因为我也注意到了我自己的代码中的错误(但是实例化的鸟没有解决它,所以我解除了这个改变......嘿,我还在学习) 。 真正的问题是,正如Berg所发现的那样,我在定义之前称之为动物,这是我不会发现使用谷歌的原因,因为我的印象是订单无关紧要!   不幸的是我的例子是错的,因为我应该调用bird.walk() 这仅在重新组织我的代码后才有效。 我写了一个javascript合并工具,递归浏览我的js类并构建js文件,然后合并到主要类别js文件中,我可以选择性地包括(bird.js,tiger.js,familytreeGrid.js,tetrisGrid.js等)。 在Bergi帮助我之前,我没有意识到订单很重要。所以这就是我的代码中的问题来自: 序列。

这是javascript文件合并脚本,对于那些感兴趣的人。

#!/bin/bash
# assumes up-to-date version of Apache is used.
# searches for /js/includes/ dir in the currently enabled website, merges the files into a <directoyname>.js, per #<directoryname> under the main dir. Walks down into subdirs recursively, thus building up a .js file with all the required #subclasses in subdirectories. This allows for sharing generic classes, by placing them higher in the directory structure. 
#Deepest directories names will become the javascript names.
# e.g. js/includes/animals.js
# js/includes/animals/birds.js
# js/includes/birds/sparrow.js
# js/inculdes/animals/insects/beetle.js
#will result in sparrow.js and beetle.js that both include animals.js, sparrow.js also has classes from birds.js
collectfromdir ()
{
local dir=$1
for subdir in $(ls -d ${dir}/*/ 2> /dev/null);do
  collectfromdir "${subdir}"
done
  if (( `ls ${dir}*.work 2> /dev/null|wc -l` ));then
    find ${dir} -maxdepth 1 -type f -name "*.work"| while read collectedFile
    do 
      (( `ls -l ${dir}*.js 2> /dev/null|wc -l` )) && cat ${dir}*.js >> ${dir}../`basename "$collectedFile"`
      cat $collectedFile >> ${dir}../`basename $collectedFile`
      rm $collectedFile
    done
   else
   upDirFile=${dir%/}.work
   #no workfile, create a file in parent dir with dirname
   (( `ls -l ${dir}*.js 2> /dev/null|wc -l` )) && cat ${dir}*.js >> ./temp.work
   (( `ls -l $upDirFile 2> /dev/null|wc -l` )) && cat $upDirFile >> ./temp.work
   mv ./temp.work $upDirFile 2> /dev/null

  fi
}

CURDIR=$(pwd)
mkdir -p $CURDIR/oldjsfiles

WEBPATH=`grep DocumentRoot /etc/apache2/sites-enabled/*.conf| cut -d ' ' -f2`
cd $WEBPATH/js/includes

for oldfile in $(ls *.js 2> /dev/null); do
  mv $oldfile $CURDIR/oldjsfiles/${oldfile}.$(date +"%Y%m$d"_`date +"%H%M%S"`)
done

#recursive call
collectfromdir $WEBPATH/js/includes
find $WEBPATH/js/includes -maxdepth 1 -type f -name "*.work"| while read f
    do
      fnew=`basename $f .work`.js
      mv $f $fnew
      chmod 440 $fnew
    done

0 个答案:

没有答案