-2
function user(){
user.name ="Amine";
user.lastName ='Meziane';
document.write(user.name);
};
user();

When executing it writes "user" only and not the name "Amine"

2
  • 2
    Did you not see the placeholder text when you entered your question. Specifically "Be specific". How do you ask a good question? Simple... follow the help guidelines. Commented Sep 20, 2014 at 23:07
  • I said it's not working I'm looking for the solution it's not something that creates debates. thank you for your comment Commented Sep 21, 2014 at 10:31

4 Answers 4

4

user refers to the user function, which comes with an existing, unassignable name property initialized to the function name. The user.name ="Amine"; assignment is ignored.

Sign up to request clarification or add additional context in comments.

6 Comments

Yep, OP probably wants new user() and this.name = ….
@Kay Or function user() { var user = {}; ... }.
but in the beginning of the JavaScript courses they say that every thing are considered as object no? so function and variables and classes are objects? thank you for commenting.
Functions are objects, but the name property of such an object isn't modifyable. Variables are alias/names referencing objects. Classes don't exist in javascript. It's a prototype based language. The Objects could be cloned. They are actually only key-value stores. A value can be an function/objects/(primitiv value).
@user3099032: If you had named your function worldSoccer, it would have written worldSoccer.
|
1

As user2357112 pointed out, your Code tries to modify the name property of the function user. This property isn't modifieable. And so it doesn't change. The name property of the function user, contains the name of the function user, which is "user" :-). And this name is what your code prints out.

You can write:

function user(){
   var user = {};
   user.name = "Amine";
   user.lastName = "Meziane";
   document.write(user.name);
};
user();

Here user(.name) will not refer to the function user, but to the local variable (var) user, which is initialized with an object literal ({}).

Maybe you wanted to write an constructor function. Then you would add properties to this.

function User(name, lastname){
  this.name = name;
  this.lastName = lastname;
  return this; //optional
};

var userAmine = new User("Amine", "Meziane"); // don't forget "new"
document.write(userAmine.name);

Maybe you can read JavaScript Patterns by Stoyan Stefanov for deeper understanding of JavaScript.

1 Comment

same with Java than but her is a function.. good thank you very much
0

Because the name of the function is read-only. Refer to documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name

Comments

-2

window.onload = function() {
function user(){
var json = '{"name":"Amine","lastName":"Meziane"}',
    obj = JSON.parse(json);
    document.body.innerHTML=obj.name+' '+obj.lastName;
}
user();
};
//or
function user(){
   var user = {};
   user.name ="Amine";
   user.lastName ='Meziane';
   document.write(user.name);
};
user();

8 Comments

Why use JSON? It seems completely superfluous; you could have used an object literal.
that's the point. I don't know which is Json and which is Pure Java. Libraries are getting me crazy lool !!! can you help me plz.. thank you
this is Json and I'm trying to learn pure Java. maybe I wrote an anti-pattern no?
JSON is Javascript, it is actually the object literal syntax with some restrictions. You pass the JSON through a parser, so you are sure the JSON Data isn't harmfull. But you could also just do 'jsonObject = eval(JSON)'.
Please don't shorten Javascript, with Java. Thats an TOTALLY different language.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.