Javascript constructor "this reference"

Tags
constructor, javascript, object, web

 
Thread Tools Search this Thread
# 1  
Old 07-19-2017
Javascript constructor "this reference"

Hello,
I just came to the object part in JavaScript, which is the instance of the object can be iterated by the for-loop (or with-loop):
Code:
function Car(seat_sth, engine_sth, radio_sth) {
    this.seats = seat_sth;
    this.engine = engine_sth;
    this.radio = radio_sth;
}
var work_car = new Car("Leather", "V-6", "Cassette/Disc");
for (var propname in work_car) {
    document.write(propname + ":    " + work_car[propname] + "<br>")
}

Instead of iterating an instance of the object, is there a way to have object constructor to traverse all object members similar to "this reference" in C++/PERL?
Code:
function Car(seat_sth, engine_sth, radio_sth) {
    this.seats = seat_sth;
    this.engine = engine_sth;
    this.radio = radio_sth;
    this.describe = describe_car();
    }
function describe_car() {
    for (var propname in this) {
    document.write(propname + ":    " + this[propname] + "<br>")
    }
}

Just started JS, and not sure I am asking a right question. Thanks!

Last edited by yifangt; 07-19-2017 at 07:24 PM..

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9" jcdole Shell Programming and Scripting 1 1 Hour Ago 06:37 AM
Excellent Oracle JET Video - "Finally, JavaScript Is Easy!" Neo What is on Your Mind? 0 10-15-2018 12:27 AM
Using "mailx" command to read "to" and "cc" email addreses from input file asjaiswal UNIX for Dummies Questions & Answers 2 10-11-2014 10:32 PM
Compiling C++ code with NetCDF libraries: "undefined reference" Alauda Programming 0 09-21-2012 02:26 PM
I got "undefined reference to" on gcc sluge Solaris 0 07-09-2012 05:57 AM
The slices "usr", "opt", "tmp" disappeared!!! Help please. wolfgang Solaris 16 05-25-2012 09:09 AM
how to use "cut" or "awk" or "sed" to remove a string timmywong Shell Programming and Scripting 8 02-12-2012 12:55 PM
make fails with "undefined reference to..." crimso Programming 6 10-19-2011 11:03 AM
Why using this kind of format in web programming <script type="text/javascript"><!-- ...//--></scrip Anna Hussie Web Programming 2 05-24-2011 12:17 AM
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 09:59 AM
cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' harshakusam Shell Programming and Scripting 4 08-25-2009 12:21 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM