JS - 对象在创建时将self添加到数组中

时间:2016-07-03 06:32:02

标签: javascript object parameter-passing

我正在尝试让js中的对象在创建时将自己添加到列表中。我想简单地添加this因为参数会起作用,但似乎由于某种原因不起作用。我该怎么做?     

<script>
function car(make, model, year, color, owner) {
    this.make = make;
    this.model = model;
    this.year = year;
    this.color = color;
    this.owner = owner || null;
    this.to_string = function() {
        return 
            "Car: " + this.color + " " + this.year + " " + this.make + " " + this.model + "\n" +
            "Owner: " + this.owner.name;
    }
    all_cars.add(this); //<---issue
    owner.buy_car(this); //<---issue
}

var all_cars = function() {
    this.list = new Array();
    this.add = function(car) {
        this.list.push(car);
    }
    this.to_string = function() {
        var r = "";
        for (var i = 0; i < this.list.length; i++) {
            r += ((r == "") ? "" : "\n\n");
            r += this.list[i].to_string();
        }
        return "ALL CARS\n" + r;
    }
}

function human(name, cars) {
this.name = name;
    this.cars = cars || new Array();
    this.buy_car = function(car) {
        car.owner = this.name;
        this.array.push(car);
    }
    this.sell_car = function(index, buyer) {
        buyer.buy_car(this.cars.splice(index));
    }
}

var vin_254s45s8wf6a = new car("Honda", "Accord", 2007, "WHT");
var vin_45d8s3g3j8x8 = new car("Toyota", "Camery", 2009, "BLU");
var vin_9s3d4f8s5w9d = new car("BMW", "JX5", 2017, "SLV");
var vin_45s6a8d5s9g2 = new car("Lexus", "CT 200h Hybrid F SPORT", 2016, "RED");
var vin_9s3d45s8wf6a = new car("Toyota", "Navta", 2001, "GRN");
var mike = new Human("Mike Smith", [vin_45s6a8d5s9g2, vin_45d8s3g3j8x8]);
var jim = new Human("Jim Hamelton", [vin_9s3d4f8s5w9d]);
var evie = new Human("Evelyn Ma", [vin_9s3d45s8wf6a]);

document.getElementById("demo").innerHTML = "HI"; //all_cars.to_string()
</script>

2 个答案:

答案 0 :(得分:0)

var all_cars = function() {
    var x = {};
    x.list = new Array();
    x.add = function(car) {
        this.list.push(car);
    }
    x.to_string = function() {
        var r = "";
        for (var i = 0; i < this.list.length; i++) {
            r += ((r == "") ? "" : "\n\n");
            r += this.list[i].to_string();
        }
        return "ALL CARS\n" + r;
    }
   return x;
}

你没有回复任何东西。对于虎人来说也是如此

答案 1 :(得分:0)

以下是我将如何构建代码(我还包括针对一系列问题的修复程序):

    #region CountFor attached property


    public static bool GetCountFor(DependencyObject obj)
    {
        return (bool)obj.GetValue(CountForProperty);
    }

    public static void SetCountFor(DependencyObject obj, bool value)
    {
        obj.SetValue(CountForProperty, value);
    }

    // Using a DependencyProperty as the backing store for CountFor.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CountForProperty =
        DependencyProperty.RegisterAttached("CountFor", typeof(bool), typeof(Window2), new PropertyMetadata(false, new PropertyChangedCallback(GetCountForChanged)));

    private static void GetCountForChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if ((bool)e.NewValue == false) return;

        Label lbl = (Label)d;
        lbl.Loaded += (o, args) =>
        {
            DependencyObject parent = VisualTreeHelper.GetParent(lbl);
            while (parent.GetType() != typeof(ListBox))
                parent = VisualTreeHelper.GetParent(parent);

            ListBox lb = (ListBox)parent;

            ICollectionView view = CollectionViewSource.GetDefaultView(lb.ItemsSource);
            lbl.Content = "Number of items = " + ((ListCollectionView)view).Count;

            view.CollectionChanged += (col, colargs) =>
            {

                lbl.Content = "Number of items = " + ((ListCollectionView)col).Count;

                System.Diagnostics.Debug.WriteLine(((ListCollectionView)col).Count.ToString());

            };
        };
    }

    #endregion