Sponsored Content
Top Forums Web Development Javascript constructor "this reference" Post 303000786 by yifangt on Wednesday 19th of July 2017 06:15:43 PM
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..
 

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

2. Web Development

Why using this kind of format in Web Development <script type="text/javascript"><!-- ...//--></scrip

I am just wondering why do programmers are using this when programming the web? When you making a joomla templates and the more focus in your mind is to target the search engines then java is very important.Not to use that. (2 Replies)
Discussion started by: Anna Hussie
2 Replies

3. Programming

make fails with "undefined reference to..."

i am compiling a program called vasp on suse and get the following error. there are many more preprocess and ifort commands prior so i just grabbed the tail of the log file: ./preprocess <main.F | /usr/bin/cpp -P -C -traditional >main.f90 -DMPI -DHOST=\"LinuxIFC\" -DIFC -Dkind8 -DNGZhalf... (6 Replies)
Discussion started by: crimso
6 Replies

4. Programming

Compiling C++ code with NetCDF libraries: "undefined reference"

Hi! I am trying to compile a C++ code with cmake and gcc on Ubuntu. The code uses NetCDF4 libraries. I specify the path to these libraries as follows: -I/usr/local/include -L/usr/local/lib -lnetcdf -lnetcdf_c++4 "ccmake" and "cmake" work fine. After typing "make" I receive the error... (0 Replies)
Discussion started by: Alauda
0 Replies

5. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

6. What is on Your Mind?

Excellent Oracle JET Video - "Finally, JavaScript Is Easy!"

This is a video well worth watching if you have any interests at all in the future of web development, web development frameworks and Javascript. https://www.youtube.com/watch?v=V8mhIEeTMCc . Fixed typo in Oracle Jet URL (oraclejet.org) (0 Replies)
Discussion started by: Neo
0 Replies

7. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies
Data::JavaScript::Anon(3pm)				User Contributed Perl Documentation			       Data::JavaScript::Anon(3pm)

NAME
Data::JavaScript::Anon - Dump big dumb Perl structs to anonymous JavaScript structs SYNOPSIS
# Dump an arbitrary structure to javascript Data::JavaScript::Anon->anon_dump( [ 'a', 'b', { a => 1, b => 2 } ] ); DESCRIPTION
Data::JavaScript::Anon provides the ability to dump large simple data structures to JavaScript. That is, things that don't need to be a class, or have special methods or whatever. The method it uses is to write anonymous variables, in the same way you would in Perl. The following shows some examples. # Perl anonymous array [ 1, 'a', 'Foo Bar' ] # JavaScript equivalent ( yes, it's exactly the same ) [ 1, 'a', 'Foo Bar' ] # Perl anonymous hash { foo => 1, bar => 'bar' } # JavaScript equivalent { foo: 1, bar: 'bar' } One advantage of doing it in this method is that you do not have to co-ordinate variable names between your HTML templates and Perl. You could use a simple Template Toolkit phrase like the following to get data into your HTML templates. var javascript_data = [% data %]; In this way, it doesn't matter WHAT the HTML template calls a particular variables, the data dumps just the same. This could help you keep the work of JavaScript and Perl programmers ( assuming you were using different people ) seperate, without creating cross-dependencies between their code, such as variable names. The variables you dump can also be of arbitrary depth and complexity, with a few limitations. ARRAY and HASH only Since arrays and hashs are all that is supported by JavaScript, they are the only things you can use in your structs. Any references or a different underlying type will be detected and an error returned. Note that Data::JavaScript::Anon will use the UNDERLYING type of the data. This means that the blessed classes or objects will be ignored and their data based on the object's underlying implementation type. This can be a positive thing, as you can put objects for which you expect a certain dump structure into the data to dump, and it will convert to unblessed, more stupid, JavaScript objects cleanly. No Circular References Since circular references can't be defined in a single anonymous struct, they are not allowed. Try something like Data::JavaScript instead. Although not supported, they will be detected, and an error returned. MAIN METHODS
All methods are called as methods directly, in the form "Data::JavaScript::Anon->anon_dump( [ 'etc' ] )". anon_dump STRUCT The main method of the class, anon_dump takes a single arbitrary data struct, and converts it into an anonymous JavaScript struct. If needed, the argument can even be a normal text string, although it wouldn't do a lot to it. :) Returns a string containing the JavaScript struct on success, or "undef" if an error is found. var_dump $name, STRUCT As above, but the "var_dump" method allows you to specify a variable name, with the resulting JavaScript being "var name = struct;". Note that the method WILL put the trailing semi-colon on the string. script_wrap $javascript The "script_wrap" method is a quick way of wrapping a normal JavaScript html tag around your JavaScript. is_a_number $scalar When generating the javascript, numbers will be printed directly and not quoted. The "is_a_number" method provides convenient access to the test that is used to see if something is a number. The test handles just about everything legal in JavaScript, with the one exception of the exotics, such as Infinite, -Infinit and NaN. Returns true is a scalar is numeric, or false otherwise. You may also access method in using an instantiated object. new HASH This will create a Data::JavaScript::Anon object that will allow you to change some of the default behaviors of some methods. Options: quote_char : Set the quote_char for stirng scalars. Default is '"'. SECONDARY METHODS
The following are a little less general, but may be of some use. var_scalar $name, $scalar Creates a named variable from a scalar reference. var_array $name, @array Creates a named variable from an array reference. var_hash $name, \%hash Creates a named variable from a hash reference. anon_scalar $scalar Creates an anonymous JavaScript value from a scalar reference. anon_array @array Creates an anonymous JavaScript array from an array reference. anon_hash \%hash Creates an anonymous JavaScript object from a hash reference. anon_hash_key $value Applys the formatting for a key in a JavaScript object SUPPORT
Bugs should be reported via the CPAN bug tracker at: http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-JavaScript-Anon <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-JavaScript-Anon> For other comments or queries, contact the author. AUTHOR
Adam Kennedy <adamk@cpan.org> SEE ALSO
JSON, <http://ali.as/> COPYRIGHT
Copyright 2003 - 2009 Adam Kennedy. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. perl v5.14.2 2012-03-26 Data::JavaScript::Anon(3pm)
All times are GMT -4. The time now is 05:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy