1。概要
コンストラクターを介してオブジェクトを作成し、新しい記述を忘れることもあるため、機能が未定義に戻ります
関数createxxxを作成して、新しい内部で新しいカプセルをカプセル化できます。
function student(props){this.name = propss.name || '匿名'; this.grade = props.grade || 1; } Student.prototype.hello = function(){alert( 'hello、' + this.name + '!');} function createStudent(props){return new Student(props || {});}関数が返品xxxを表示しない場合。未定義を返します。
例
コンストラクターを使用して猫を定義し、すべての猫オブジェクトに名前属性を持たせ、メソッドを共有する() "Hello、xxx! '' Hello、xxx! 'を返す方法()を共有します。
'strict'; function cat(name){this.name = name;} cat.prototype.say = function(){return( 'hello、' + this.name + '!');} // test:var kitty = new cat( 'kitty'); kitty.say && typeof kitty.say === 'function' && kitty.say()=== 'hello、kitty!'古典的なJSオブジェクト作成モデルに関する上記の記事は、私があなたと共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。