5

JavaScript では、いくつかの異なる方法が見られます。特定のタスクをオブジェクト内で実行できます。たとえば、下にある卵のオブジェクトです。

誰も私にそれぞれの違いを教えてもらえますか、なぜ一方を使用し、他方を使用しないのかなど

 var Egg = function(){

    //Properties

    var shell = "cracked" // private property 

    this.shell = "cracked" // public property

    shell: "cracked" // what is this??

    //functions

    function cook(){

        //standard function
    }

    cook: function(){
        //what kind of function is this?
    }

    //not sure what this is

    details: {
        //What is this? an array :S it holds 2 elements?
        cost: 1.23,
        make: 'Happy Egg';
    }




}
4

4 に答える 4

4

確かに、ベン。

この種のことは、JavaScriptのダイナミズムの根底にあります。まず、基本を見ていきます。たとえば、JavaやC ++ / C#などのクラスベースの言語を理解している場所から来ている場合、最も理にかなっているのはコンストラクターパターンです。これは非常に早い段階で含まれていました:

function Egg (type, radius, height, weight) {
    // private properties (can also have private functions)
    var cost = (type === "ostrich") ? 2.05 * weight : 0.35 * weight;

    // public properties
    this.type = type;
    this.radius = radius;
    this.height = height;
    this.weight = weight;
    this.cracked = false;

    // this is a public function which has access to private variables of the instance
    this.getCost = function () { return cost; };
}

// this is a method which ALL eggs inherit, which can manipulate "this" properly
// but it has ***NO*** access to private properties of the instance
Egg.prototype.Crack = function () { this.cracked = true; };


var myEgg = new Egg("chicken", 2, 3, 500);

myEgg.cost; // undefined
myEgg.Crack();
myEgg.cracked; // true

それは問題ありませんが、物事を回避するためのより簡単な方法がある場合があります。時々あなたは本当にクラスを必要としません。

必要なレシピはこれですべてなので、たまごを1つだけ使用したい場合はどうでしょうか。

var myEgg = {};  // equals a new object
myEgg.type = "ostrich";
myEgg.cost = "......";
myEgg.Crack = function () { this.cracked = true; };

それは素晴らしいことですが、そこにはまだ多くの繰り返しがあります。

var myEgg = {
    type : "ostrich",
    cost : "......",
    Crack : function () { this.cracked = true; }
};

2つの「myEgg」オブジェクトはどちらもまったく同じです。

ここでの問題は、myEggのすべてのプロパティとすべてのメソッドが100%誰にでも公開されていることです。

その解決策は、すぐに呼び出す関数です。

// have a quick look at the bottom of the function, and see that it calls itself
// with parens "()" as soon as it's defined
var myEgg = (function () {
    // we now have private properties again!
    var cost, type, weight, cracked, Crack, //.......

    // this will be returned to the outside var, "myEgg", as the PUBLIC interface
    myReturnObject = {
        type : type,
        weight : weight,
        Crack : Crack, // added benefit -- "cracked" is now private and tamper-proof
        // this is how JS can handle virtual-wallets, for example
        // just don't actually build a financial-institution around client-side code...
        GetSaleValue : function () { return (cracked) ? 0 : cost; }
    };

    return myReturnObject;
}());

myEgg.GetSaleValue(); // returns the value of private "cost"
myEgg.Crack();
myEgg.cracked // undefined ("cracked" is locked away as private)
myEgg.GetSaleValue(); // returns 0, because "cracked" is true

それがまともなスタートであることを願っています。

于 2012-07-19T17:33:30.280 に答える
1

オブジェクト プロパティの宣言と単純な JavaScript ステートメントの間で構文を混在させています。

// declare an object named someObject with one property
var someObject = {
    key: value
};

// declare an anonymous function with some statements in it
// and assign that to a variable named "someFunction"
var someFunction = function () {
    // any javascript statements or expressions can go here
};
于 2012-07-19T17:12:19.830 に答える
0

JavaScript では、オブジェクトと関数の間に重要な違いがあります。オブジェクトは一連のデータ (関数を含む) を保持し、関数を使用してオブジェクトを作成または変更できますが、それらは本質的に同じものではありません。JavaScript の OOP は、関数をクラスとして使用することに基づいています。たとえば、次のクラスを取り上げます。

Test = function(){
    this.value = 5;
}

関数を呼び出すだけTest()では、何も起こりません。と言ってもvar x = Test()、xの値は になりますundefined。しかし、newキーワードを使うと魔法が起こります!したがって、 と言うとvar x = new Test()、変数xには Test オブジェクトが含まれます。を実行するとconsole.log(x.value)、5 が出力されます。

これが、関数を使用してオブジェクトを作成する方法です。また、構文が異なるキーもあります。関数には、if ステートメントや for ループなど、任意の種類の JavaScript ブロックを含めることができます。ただし、オブジェクトを宣言するときは、key: value構文を使用する必要があります。

少しでも問題が解決することを願っています!

于 2012-07-19T17:14:37.307 に答える