Difference Between The Object Literal And The New
Jul 2, 2010
I have read this from the interentfunction getArea(radius)
Imagine we want to create lots of circle objects using our object as a base - if we declare using the object literal, were not able to do that.
But the new operator can do that - basically allowing us to re-use a user-defined object.
can't understand on the above why we declare using the object literal not able to do that.
View 8 Replies
ADVERTISEMENT
Oct 9, 2010
I have the following code:
A = {
"value": "a",
};
B = A ;
B.value = "b";
alert (A.value); // => b
I expected A.value to be "a". how can I change values of a new object, without changing the parent-object?
View 6 Replies
View Related
Oct 17, 2006
Below I declared a basic object literal with 2 methods. The
"doSomething" method is call from the "useDoSomething" method but the
call is only sucessful if I use the "this" keyword or qualify the call
with "SomeObj".
Can someone describe why this is happening?
var SomeObj = {
doSomething : function()
{
return 'Did something'
},
useDoSomething : function()
{
//Doesn't work - doSomething() no defined.
return doSomething();
//Works
//return this.doSomething();
//Works
//return SomeObj.doSomething();
}
}
alert(SomeObj.doSomething());
alert(SomeObj.useDoSomething());
View 11 Replies
View Related
Oct 2, 2007
I am having a hard time getting the javascript file to run any of the functions after I've changed my original script to object literal format. Please tell me why it isn't working! I have attached a zip format of my code.
View 4 Replies
View Related
Jul 1, 2009
I'm declaring a class in OLN:
Code:
The problem is that in IE, when I call myfunc() it complains with the error: Class is undefined
I'm not sure what I'm doing wrong. Class is declared in as much global scope as possible, and that shouldn't even matter in JS.
View 3 Replies
View Related
Sep 22, 2009
I have this below code snippet
I have an object myscript and from within the method validate, i want to call the method init. so is there there anyway that I can call the method init without having to prefix like this: myscript.init();
I tried this.init(); but it's not working
Code:
View 15 Replies
View Related
Aug 18, 2010
i have something like this:
Code:
var o = {
f1:function(a) {
[code]....
View 4 Replies
View Related
Mar 30, 2011
Consider the following:
Code:
myObjLit = {
prop1:null,
[code]....
View 2 Replies
View Related
Apr 13, 2010
I'm attempting to understand the use of privileged methods when used with Object.create. I put together a quick demo (code below) that shows what happens when I use Object.create to create a new object based on one that has a privileged method. The outcome is not pleasant, as changing the value in the first object also changes it in the second. Unless I am reading Crockford's articles incorrectly, this makes Object.create almost useless for anything but objects that have only public members.
I've seen many JavaScript programmers use closures extensively to create private members, and that still holds to be a good programming practice. However I still can't find an elegant way to create inheritance in combination with closures in JavaScript, given downfalls such as the one I mentioned above.With all of that said I still think Crockford has a nice way of programming, creating factory functions that produce objects, staying away from the prototype property and making the language look more functional.
Here's the code to demonstrate what I'm referring to. Firebug needs to be enabled to view the console.debug output, otherwise convert them to alerts.
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
[code]....
View 3 Replies
View Related
Nov 22, 2010
what is the best way to setup one literal to be the prototype for another?So far I can only think of making a copy into a constructor.
Code JavaScript:
var meths = {
// creates a constructor function populated with a shallow copy of child
// the prototype for the new instance is set to the parent.
[code]....
View 8 Replies
View Related
Nov 25, 2011
According to ECMAScript, the root of the prototype chain is Object.Prototype. Each object has an internal property [[Prototype]] that could be another object or NULL.... However, it also says that every function has the Function prototype object: Function.Prototype, it confused me, because a function is an object, for a function object, what is its function prototype and object prototype..For example:
var x = function (n) {return n+1;};
what is the relationships of x, Object.Prototype and Function.Prototype
View 5 Replies
View Related
Jun 6, 2006
How would one make the ECMA-262 String.replace method work with a
string literal?
For example, if my string was "HELLO[WORLD]" how would I make it work
in this instance.
Please note my square brackets are not regular expression syntax.
View 21 Replies
View Related
Mar 16, 2007
A section of my webpage consists of two dozen lines of text displayed
by sequentially calling:
function printText1( text )
{
document.getElementById( "textLine1" ).innerHTML = text;
}
....
function printText24( text )
{
document.getElementById( "textLine24" ).innerHTML = text;
}
Is there a way to parameterize the string literal (textLineXX) so that
I can do it all by repeatedly calling one printText function?
View 7 Replies
View Related
Jun 14, 2010
I have the following code which attaches a function to events in x number of comboboxes (x will probably always = 4, but I do not want to hard-code this). I wish to pass the value of i to the function being attached as well as the value of tempData. In other words, I want the parameters in function to be the value, not a reference variable.
In the current example, I am using the hard-coded variable ci. This I want to be replaced by a literal created when the event handler is attached (the value of the loop variable i). Also, notice that I get the filter value in the event handler (assigned to the variable ct). I would like to replace this code with the value of tempData which would also be determined when the evenet is attached (it is the same value in this case, but it keeps the onChange event from having to do this each time it runs).
var props = {
col_0: "select",
col_1: "select",
[code]....
View 6 Replies
View Related
Jan 25, 2006
im trying to read an xml file and put it into a textarea on my page.
here is the code that returns the xml file as a string:
Document doc = new Document();
SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
doc = saxBuilder.build(filesName);
String xml = outputter.outputString(doc);
no when i try to pass this to a javascript as follows:
function putXML(fileName){
var blah = '<%= xml %>'
document.btraxtestForm.HighwayMessage.innerText=blah;
}
it gives me a unterminated string literal error.
also, i will never know the contents of this xml file so i cant split up the string.
has anyone any ideas how i will fix this?
View 5 Replies
View Related
Aug 17, 2010
I've started learning writing javascript objects with literal notation...
Instead of the usual syntax...
The problem is that it's hard for me to adapt procedures which I'm used to within the normal syntax for the literal notation. E.g:
Is this variable global or local? If it's local, how do I declare a variable globally? I also wonder if it is possible to bind events to objects from within the object? And if I'm correct - jQuery is coded this way, but how can they then make use of their selector:
When it is an object defined with literal notation that doesn't have a constructor?
View 2 Replies
View Related
Jan 2, 2011
There are two arrays named myContacts and getRes. When I use myContacts array it works fine. When I assign some value for getRes array which is exactly similar format like mycontacts array, but it’s a dynamic value which I already checked no error In the data content. But am facing problem with dynamic value which gave me not same as myContact. tq
var myContacts = "";
var getRes = "";
[code]....
View 2 Replies
View Related
Oct 19, 2005
Below is a stripped down example of code I'm using to count links in a
list. My intent with the ShowLinkNumber() function was to display the
number of the current link. (e.g. click on link 2, and the message "2"
pops up; click on link 4, and you see a "4.")
For reasons I can't trace, my function always displays the last number
in the list. Why does it do this, and can anyone suggest a way to
achieve my goal without adding markup? Code:
View 3 Replies
View Related
Jun 23, 2006
The XML my PHP app is returning to my JavaScript function has elements in
it that contain special characters... specifically the ampersand & which
hoses up the data. ex: Company Name = "K & B Construction".
Can anyone give me some idea of how I make the data coming out of my
database translate as literal characters in the XML output?
View 5 Replies
View Related
Jan 24, 2009
Error: unterminated string literal
on this line?
document.getElementById('id_company').innerHTML = '<span class="textGreen"><strong>news</strong></span>';
View 1 Replies
View Related
Jun 30, 2011
I created a simple WYSIWYG for creating CSS based webpages. It works exactly as assumed in Safari and Chrome. However, in firefox, when you copy/paste more than one line of text, instead of formatting it as html, in puts in line breaks causing the array storing all of the data to break into multiple lines and causing an unterminated string literal.
1. suggest why it is behaving this way?
2. suggest a work around to allow it to work on FF?
View 2 Replies
View Related
Oct 8, 2011
i am getting this error " unterminated string literal" but iam unable to locate the problem..iam using fire bug for firefox..
View 6 Replies
View Related
Apr 24, 2010
I've been banging on this for a couple hours even though I'm sure it has a less-than-five-second fix. Hopefully somebody will glance at it and point out the obvious thing I'm missing.I'm using the onchange event of an html select element to trigger an array prototype function that looks for a value in an array and returns the indices of the array that contain that value, or FALSE if the array doesn't contain the value. For this example, here's the array being set up:
var instids = new Array();
instids[0] = 5;
instids[1] = 6;
[code]....
View 1 Replies
View Related
Dec 31, 2009
How do I start my literal object as a function like you do when you use the new Object method.
e.g. var fn = function(){ ...code...}
var func = new fn();
I'm trying to achieve this using the literal object method like:
var fn ={
key: ''
}
View 2 Replies
View Related
Apr 1, 2011
Am I able to pass a function literal (at least I think that's what it's called) to a jquery event handler, and retrieved the event details in the function?
[Code]...
View 2 Replies
View Related
Dec 22, 2007
I can't find anything wrong with this code <!-- Initialise the editor --><script>
initRTE('<table align="" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr></tr></tbody></table>', 'example.css');
</script>
However this throws an error in firebug and the page doesn't display it correctly, i get an error in firebug
unterminated string literal
initRTE('<table align="" border="0" cellpadding="0" cellspacing="0" widt...
Does anyone have and idea whats going on?
View 16 Replies
View Related